Why do some classes require main methods and others do not?
class, java, oop
Solution
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.
Problem
I'm currently enrolled in an online Java class, and my instructor has led me to believe that all Java classes must have a main method ie. ``` public class { public static void main(String[] args) } ``` However, we've just reached a unit on cross referencing classes in other files where this is not the case. Ex. ``` public class Pie { // declare variables to be called in separate file String type; int diameter; float radius; } ``` Pie can then be referenced in a fashion such as: ``` Pie newPie = new Pie(); System.out.println("What type of pie will you be eating today?"); newPie.type = in.readLine(); System.out.println("Ah. " + newPie.type + ". Excellent choice.\n"); ``` And this works fine. However the explanation behind why this functions properly eludes me. Can anyone please explain?