0

Good day,

I am fairly new to Java. The first language I started learning was C++. With C++ I used a new .cpp file to spread my code to multiple files, so it doesn't get too clustered. I would only use classes when needed (for example categorizing students from a school by their name, address, phone number etc.). But now that I changed to Java, I have trouble understanding how to use classes, when I don't want to categorize something. Lets say I want to spread my code in multiple .java files, so it doesn't get clustered. When creating a new .java file, it automatically creates a new class. So my question would be: If I don't plan using the class the conventional way, what would be the solution: declaring all methods and variables as static inside the new class in the new .java file, or create only one instance of the class and use this instance through the rest of the code? And is the correct approach (using static vs instance) still correct when using JavaFX?

Stefan B
  • 541
  • 2
  • 6
  • 13

1 Answers1

0

Java is much more object-oriented than C++ to the point where code essentially can't exist outside of an object or class. Like redFIVE, I recommend you look at a few java tutorials to get used to the paradigm of Java programming and object-oriented design.

Essentially, the idea is this: You can reduce your entire idea for a project into classes and objects. Objects have states and behaviors which may or may not interact with one another. In your example, a Student might belong to a School and have Registrations for Courses which have Sessions to expand a Student's Knowledge, and on and on and on. There's no code that exists outside of these objects (and the classes and interfaces that support them).

Java and C++ have different coding paradigms which can make it difficult to code well across both at first. Check out some tutorials and complex examples before diving into StackOverflow for help.

Zircon
  • 4,677
  • 15
  • 32