+ 4

What is the use of "static" in public static void main(String[] args) ?

23rd Jun 2018, 11:05 AM
Ajay Parmar
Ajay Parmar - avatar
2 Réponses
+ 7
Because, in Java a function or variable in class in unaccessible untill an Object is created from the class, but the 'main' function is supposed to run at startup(without Object initiation) by JVM. So the 'main' is declared public as well as static so that it can be accessed outside class & without even an Object is created. https://www.quora.com/Why-do-we-use-%E2%80%9Cpublic-static-void-main%E2%80%9D-only-in-Java
23rd Jun 2018, 11:08 AM
MsJ
MsJ - avatar
+ 5
static helps the main method to get loaded without getting called by any instance/object.
23rd Jun 2018, 11:18 AM
Prince Raj