+ 1

main and class in Java

Why is it necessary to put the main method inside of a class, what is difference between the class where the main is in and other classes which do not contain the main method?   For a file that has more then one class, can I put the main method in one of those classes?

28th Jan 2018, 7:41 AM
J4de
J4de - avatar
1 Answer
+ 10
For your very first question: https://stackoverflow.com/a/2155407 Methods define behavior of the class. main() method must appear within a class, but it can be any class. It is the starting point of any Java program i.e. all the other methods in you program and their fields get executed inside of main() (if they're not called in main(), they won't get executed). You can check for these stuff here: https://www.sololearn.com/learn/Java/2152/ You Java program can contain more than one main(), but only if each of them has different parameters. Also known as Method Overloading. Check https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html I'd also recommend going through this link, https://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html
28th Jan 2018, 8:07 AM
Dev
Dev - avatar