Possible Duplicate:
Why is the Java main method static?
What's the reason of main method to be static? Why isn't just public void main(String[] args)
? I think I understand what static means, but I see no reason to be it here. Thank you.
Possible Duplicate:
Why is the Java main method static?
What's the reason of main method to be static? Why isn't just public void main(String[] args)
? I think I understand what static means, but I see no reason to be it here. Thank you.
We declare, main method in java as : public static void main(String args[])
static : main is the entry point of a class. In java everything is written in a class.Now when you run java on command prompt, loader will load the class and jvm will search the main method to enter into the class. so making the main() as static, will make jvm access it directly without creating the instance.
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.
Remember that everything in java is a class, and to give the JVM direct access to the main method without creating the object it is contained in it has to be static.
static : main is the entry point of a class. In java everything thing is written in a class. Now when you run java filename on command prompt, loader will load the class and jvm will search the main method to enter into the class. so making the main() as static, will make jvm access it directly through classname.main()
This is also a good resource for this question, located here
According to my limited Java knowledge, main()
is static because when a Java application is launched, it does not create any class instances. There needs to be a function that can be called without creating an instance, which is exactly what the static keyword does.