Welcome back to Java Code Online. One of the most basic conception for a fresher is to put a main method in each Java class he or she writes. Most often they never know that, not all Java Class need a main method.
Java is an Object Oriented language based on the Principle of OOPS. The basic principle of OOPS is to visualize everything as an Object. The best coding practice is to make a separate class for each Object that is required by your Java Code.
The "main" method is required to start the program. Whenever the Java program is executed, the JVM looks for the method containing the "main" method. It gets it in the form of public static void main(String args[]). This method tells the JVM that ok, I am the main method and you need to start reading the code from me.
There could be multiple classes with different methods serving different functionality, though none of them may contain a "main" method. There could be a single class which controls or more specifically uses these other classes, make their Objects and use them in the Java Code.
So, now I think that I am clear. It is not compulsory for every class in Java to have a "main" method, though there has to be class with a main method which uses uses these classes.
I hope the article was beneficial in understanding the requirement of the "main" method in the Java Code. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.