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?

Re: Why is the main method static? 80 80

Dunno probly cus thats how they want it to work.

Re: Why is the main method static? 80 80

i think static has to do w/ how its accessed

Re: Why is the main method static? 80 80

i think static has to do w/ how its accessed

I already knew that.

Re: Why is the main method static? 80 80

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

Re: Why is the main method static? 80 80

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

Re: Why is the main method static? 80 80

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

Re: Why is the main method static? 80 80

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

Re: Why is the main method static? 80 80

And be potentially impossible.
Which constructor should the JVM take after all?

Re: Why is the main method static? 80 80

Which constructor should the JVM take after all?

The one it likes most?

Re: Why is the main method static? 80 80

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 Sun's site

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;
}
}
Re: Why is the main method static? 80 80

>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. ;)

Re: Why is the main method static? 80 80

What exactly is that rule your talking about?

If you guys find any readings on this, please post URL's.

Re: Why is the main method static? 80 80

java.sun.com is your friend. :)

Re: Why is the main method static? 80 80

java.sun.com is your friend. :)

I never knew I had any friends?

Re: Why is the main method static? 80 80

Me either I thought you just sat at your computer all day. JK

Re: Why is the main method static? 80 80

Me either I thought you just sat at your computer all day. JK

I do? There's no joking in that.

Re: Why is the main method static? 80 80

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.

Re: Why is the main method static? 80 80

Thank you, I like the link :cheesy:

Re: Why is the main method static? 80 80

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/

Re: Why is the main method static? 80 80

i wanna be a good java developer

Re: Why is the main method static? 80 80

i wanna be a good java developer

Join the club. I've been programming for 8 years now and I still want to move from beginner to good.

Re: Why is the main method static? 80 80

Join the club. I've been programming for 8 years now and I still want to move from beginner to good.

The first step in that process you've already made, realising you have things to learn :cool:

Re: Why is the main method static? 80 80

Along similar lines, whhy is main method in Java required to be declared as "void" ?

Re: Why is the main method static? 80 80

main method is static because ,
when loader loads a class file into command promot and jvm search the main method and it found static so we can called that class withod creating an object from the instance of the class.

and also jvm gets main() then objet instantiated there is accessed.

Re: Why is the main method static? 80 80

This is 6 years old thread!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.