-3

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.

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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()

Reference here

This is also a good resource for this question, located here

2 Comments

Since main method is static JVM can call it without creating any instance of class which contains main method. 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 way for JVM to find main method in Java.
That's mostly pointed out in the answer. Which is 3 years old.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.