Main method in Java is the first programming method a Java programmer knows when he starts learning Java programming language.have you ever thought about why main method in Java is public, static and void, of-course Yes, since most of us first learn C and C++ than we move to Java in our programming path we familiar with main method but in Java main method is slightly different it doesn't return any value like in C it returns int, main method is public static and void Why? In this post we will try to find answer to these questions and have an idea of one of the most popular questions in Java why main method is declared Static.
What is the main method in Java?
Main method in Java is entry point for any core Java program. Remember we are not talking about Servlet, MIDlet or any other container managed Java program where life cycle methods are provided to control the execution. In core Java program, execution starts from main method when you type java main-class-name, JVM search for public static void main(String args) method in that class and if it doesn't find that method it throws error NoSuchMethodError:main and terminates.
Signature of the main method in Java
The main method has to strictly follow its syntax; other wise JVM will not be able to locate it and your program will not run. Here is the exact signature of the main method
public static void main(String args)
This signature is classic signature and there from start of Java but with introduction of variable argument or varargs in Java5 you can also declare the main method in Java using varargs syntax as shown in below example:
public static void main(String... args)
Remember varargs version of java main method will only work in Java 1.5 or later version. Apart from public, static and void, there are certain keywords like final, synchronized and strictfp which are permitted in the signature of java main method.
Why main method is static in Java
Now come to the main point "Why the main method is static in Java", there are quite a few reasons around but here are few reasons which make sense to me:
1. Since the main method is static Java virtual Machine can call it without creating any instance of a class which contains the main method.
2. Since C and C++ also have similar main method which serves as entry point for program execution, following that convention will only help Java.
3. If main method were not declared static than JVM has to create instance of main Class and since constructor can be overloaded and can have arguments there would not be any certain and consistent way for JVM to find main method in Java.
4. Anything which is declared in class in Java comes under reference type and requires object to be created before using them but static method and static data are loaded into separate memory inside JVM called context which is created when a class is loaded. If main method is static than it will be loaded in JVM context and are available to execution.
Why main method is public in Java
Java specifies several access modifiers e.g. private, protected and public. Any method or variable which is declared public in Java can be accessible from outside of that class. Since the main method is public in
Java, JVM can easily access and execute it.
Why the main method is void in Java
Since the main method in Java is not supposed to return any value, it's made void which simply means main is not returning anything.
1. The main method must be declared public, static and void in Java otherwise, JVM will not able to run Java program.
2. JVM throws NoSuchMethodException:main if it doesn't find the main method of predefined signature in class which is provided to Java command. E.g. if you run java Helloworld than JVM will search for public static void main String args) method in HelloWorld.class file.
3. The main method is an entry point for any Core Java program. Execution starts from the main method.
4. The main method is run by a special thread called "main" thread in Java. Your Java program will be running until your main thread is running or any non-daemon thread spawned from the main method is running.
5. When you see "Exception in Thread main” e.g.
Exception in Thread main: Java.lang.NullPointerException it means Exception is thrown inside main thread.
6. You can declare the main method using varargs syntax from Java 1.5 onwards e.g.
public static void main(String... args)
7. Apart from static, void and public, you can use a final, synchronized and strictfp modifier in the signature of the main method in Java.
8. The main method in Java can be overloaded like any other method in Java but JVM will only call the main method with specified signature specified above.
9. You can use throws clause in the signature of the main method and can throw any checked or unchecked Exception.
10. A static initializer block is executed even before JVM calls the main method. They are executed when a Class is loaded into Memory by JVM.