+ 3
why the main method in Java is always static ?
5 Réponses
+ 6
This is necessary because main() is called by the JVM before any object is made.since it is static it can be directly invoked via the class.
+ 4
because we don't need to create an instance from it
+ 3
because for a static method there is no need to create an object
+ 1
1. by rule of Java, you should write the same syntax defined for main().
2. static members are loaded first during the execution, so main has to be static as the execution starts from there only.
3. if it is not declared as static compiler throws error, cz its not according to the syntax.
Also, you can see that we never create any instance for the main function, since it has to be static.
+ 1
Suppose if we do not make main method static then we have to create the object of main method. but the question arises is where we create the object of main method. That's why we make the main method static so that it can be accessed by the class name and we don't need to create the object of main method.