Why main method declared public and static in Java
Why does the main method have to be declared public and static in Java? What do these keywords mean and what are their implications? In this post, we will explain the reasons behind this syntax and how it affects the behavior of the main method.
1. Syntax of main method in Java
We know that the main method which serves as an entry point for the application. The Java Virtual Machine (JVM) starts its execution by invoking the main method of the specified class, and main() will subsequently invoke all the other methods required by the program. The main method has a specific syntax that looks like this:
|
1 2 3 |
public static void main(String[] args) { // code } |
The syntax of the main method is fixed and cannot be changed. If we try to modify it or use a different name, the JVM will not recognize it and the program will not run. Therefore, we have to follow this syntax and use this name for our main method in every Java application.
2. Why is main method public in Java?
Public is an access modifier that determines who can access or invoke a method or a class. By declaring the main method as public, we are allowing it to be accessed by anyone, including the JVM, which is responsible for running Java applications.
The JVM needs to access the main method to start the execution of the program. If the main method is not public, the JVM will not be able to find it or call it, and the program will not run. Therefore, it is required to declare the main method as public to make it visible and accessible to the JVM.
3. Why is main method declared static in Java?
Static is a keyword that indicates that a method or a variable belongs to the class itself, not to any specific instance of the class. By declaring the main method as static, we are telling the JVM that it can call the main method without creating an object of the class that contains it.
This is important because when the JVM starts running a Java application, there is no object of any class available yet. The JVM only knows the name of the class that contains the main method, which is specified in the command line or in the manifest file of a JAR file. Therefore, the main method is declared static in Java to have JVM directly invoke it without instantiating the object of the class.
If the main method is non-static, then JVM needs to create an instance of the class, and there would be ambiguity if the constructor of that class takes an argument – which constructor should be called by JVM and what parameters should be passed? We know that JVM can’t instantiate a Java class without calling a constructor method. We can agree upon a default constructor, but that’s extra overhead. Also, the class must not be abstract; otherwise, the JVM could not instantiate it. To make Java a little less verbose than it already is, the main method is static in Java.
4. Why is main method return void in Java?
Void is a keyword that indicates that a method does not return any value. By declaring the main method as void, we are telling the JVM that it does not need to expect any value from the main method after its execution.
The main method does not return any value because it is not meant to be called by other methods or classes. It is only meant to be called by the JVM as the starting point of the program. Therefore, returning any value from the main method is meaningless and unnecessary.
If the main method returns any value, such as int or String, we will get an error “Main method must return a value of type void in class Main, define the main method as: public static void main(String[] args)”. Therefore, it is recommended to declare the main method as void to avoid confusion and redundancy.
That’s all about why the main method is declared public and static in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)