954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
 

Dunno probly cus thats how they want it to work.

OurNation
Master Poster
780 posts since Aug 2004
Reputation Points: 16
Solved Threads: 9
 

i think static has to do w/ how its accessed

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 
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
Administrator
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
Administrator
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
Team Colleague
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'm not much of a C++ person, but I believe a different angle answer to your question might be found in the file java.c.

I downloaded the J2SE sources from the Sun's site:
http://www.sun.com/software/communitysource/j2se/java2/download.xml

I decompressed the archive and I took a look at the java.c file, from:
jdk-1_5_0-src-jrl\j2se\src\share\bin

I also found a possible answer to the unasked question
"Must the "main" method be public?"

{ /* Make sure the main method is public */
jint mods;
jmethodID mid;
jobject obj = (*env)->ToReflectedMethod(env, mainClass,
mainID, JNI_TRUE);

if( obj == NULL) { /* exception occurred */
ReportExceptionDescription(env);
goto leave;
}

mid =
(*env)->GetMethodID(env,
(*env)->GetObjectClass(env, obj),
"getModifiers", "()I");
if ((*env)->ExceptionOccurred(env)) {
ReportExceptionDescription(env);
goto leave;
}

mods = (*env)->CallIntMethod(env, obj, mid);
if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
message = "Main method not public.";
messageDest = JNI_TRUE;
goto leave;
}
}

LunLun
Newbie Poster
4 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

>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
Administrator
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
Administrator
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

OurNation
Master Poster
780 posts since Aug 2004
Reputation Points: 16
Solved Threads: 9
 
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
Team Colleague
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
 

Told you the JLS is your friend :p

If you want more obscurity, here's the virtual machine specifications: http://java.sun.com/docs/books/vmspec/

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You