Java is an object-oriented language.
A Java project should always contain a src folder and a test directory.
For example, if I have a project called MyFirstShape, then there would be a folder called MyFirstShape and inside that folder, all of the folders and files generated to run java would be stored.
MyFirstShape folder
– src folder (contains all the codes used to run the app)
– MyFirstShape.java (contains the main method used to run the app, recommend not to use the class for any other purpose than having the main method)
– Shape.java (a class that the app uses)
– test folder (contains all the codes used to test the app for quality assurance)
– ShapeTest.java (a class for testing the Shape class)
A project is composed of classes. The class can be for the main method or for creating objects that the program needs to use.
Main Method
The main method is what the Java compiler runs when you “run” your program . Thus, you must have a main method in order for your program to actually do something other than just create the blueprints that can be used to create objects (source: Head First Java).
Although there is no rule saying that you must keep your main method separate, it is recommended because (source):
– A class should have a single responsibility to reduce confusion. Thus, a class containing the main method should only have one responsibility, to allow the program to start from the command line.
– In more complex programs, it may be necessary to have multiple main methods with different parameters, special modes, etc.
– You may want to restrict access to the main method when running the program from a different environment (i.e., Eclipse plugin, an applet, a web based tool, etc.). Not possible to restrict access if the class contains methods for other functionalities that must remain public.