Why is the main method static?
I've often pondered over this subject. I know the main method has a special signature the JVM looks for, but why is it static?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
i think static has to do w/ how its accessed
I already knew that.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
>why is it static?
Primarily so that the JVM doesn't have to instantiate your controlling class to get an entry point into the program.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>why is it static?
Primarily so that the JVM doesn't have to instantiate your controlling class to get an entry point into the program.
So that would take longer than having a main method that does the same thing?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
>So that would take longer than having a main method that does the same thing?
If main isn't static then an object of the class must be created first (unless the JVM wants to break its own rules, which isn't unheard of). By requiring main to be static, the environment avoids an unnecessary step. So yes, it would take slightly longer.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>So that would take longer than having a main method that does the same thing?
If main isn't static then an object of the class must be created first (unless the JVM wants to break its own rules, which isn't unheard of). By requiring main to be static, the environment avoids an unnecessary step. So yes, it would take slightly longer.
Thank you Narue, I believe I understand now.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
And be potentially impossible.
Which constructor should the JVM take after all?
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Which constructor should the JVM take after all?
The one it likes most?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
>I also found a possible answer to the unasked question
There's no need to ask it when the asked question was "Why must main be static?". The language specification says that main has to be static, but doesn't explain why in any detail.
>And be potentially impossible.
Theoretically, the JVM could break its own rules (and the rules of OOD) and simply look for a main method as the entry point. But that's more effort than it's worth from my experience implementing object oriented language compilers. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
What exactly is that rule your talking about?
If you guys find any readings on this, please post URL's.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
java.sun.com is your friend. :)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
java.sun.com is your friend. :)
I never knew I had any friends?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Me either I thought you just sat at your computer all day. JK
I do? There's no joking in that.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Java Language specification (second edition) section 12.1.4:
12.1.4 Invoke Test.main
Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.
The method main must be declared public, static, and void. It must accept a single argument that is an array of strings. http://java.sun.com/docs/books/jls/ a mandatory link for everyone using Java, it's the final word on everything.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Thank you, I like the link :cheesy:
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337