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?

Dunno probly cus thats how they want it to work.

i think static has to do w/ how its accessed

i think static has to do w/ how its accessed

I already knew that.

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

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

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

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

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

Which constructor should the JVM take after all?

The one it likes most?

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;
}
}

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

What exactly is that rule your talking about?

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

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

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

I do? There's no joking in that.

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.

Thank you, I like the link :cheesy:

i wanna be a good java developer

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.

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:

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

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.

This is 6 years old thread!

Be a part of the DaniWeb community

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