Why do some classes require main methods and others do not?

Every Java program (which is in turn, built up from one or more Java classes) requires a Main method. The purpose of this special method is to serve as an entry point to your program so that your program can be executed. More information can be found in this page.

In your Pie example, what happens is that when you run your application, the main method will be the first thing which will be called. Once that it will be called, it will create a new Object, named newPie using the Pie template (class) and so on.

Just as extra information, if you are using an IDE, if you add a main method in your Pie class with the given signature: public static void main(String[] args), the next time you run your program the IDE will ask you to select your entry point since it will have now found two entry points. Once that you will have made your selection, the IDE will make the necessary configurations so that the entry point of your application is noted.

Leave a Comment