static

Hi,when we execute a java program , we say the execution starts from main() method.but when we run the program , we are not calling the main() method.then how this happens.?thanks.
[215 byte] By [padikkathavana] at [2007-11-27 2:41:58]
# 1
> then how this happens.?The JVM calls the main() method.~
yawmarka at 2007-7-12 3:06:18 > top of Java-index,Java Essentials,New To Java...
# 2
JVM invokes the main method for the specified class. For more details refer to java lang spec.
AnanSmritia at 2007-7-12 3:06:18 > top of Java-index,Java Essentials,New To Java...
# 3

> but when we run the program , we are not calling the

> main() method.

> then how this happens.?

The name of the starting class is given as a parameter to the Java runtime system when a program is started. The runtime system then will look for the main method in the specified class. You can have many main methods in a Java program. The one that gets called is the one you specify to the runtime system when the program is started. This is also where you specify the parameters to the String array in the main method.

AnanSmritia at 2007-7-12 3:06:18 > top of Java-index,Java Essentials,New To Java...
# 4

You can have many main methods in a Java program. The one that gets called is the one you specify to the runtime system when the program is started. This is also where you specify the parameters to the String array in the main method.

========

say, we have more than one main() method in the java program.

how we have to mention the particular main() method to the java runtime system ?

can you explain a litte bit further?

=========

padikkathavana at 2007-7-12 3:06:18 > top of Java-index,Java Essentials,New To Java...
# 5

It means main method can be overloaded. But the JVM will look for

public static void main(String[] args) or

public static void main(String... args)

if the same class contains the another main method such as

public static void main(int i) {

}

JVM will not invoke this method. You have to manually invoke in your program.

AnanSmritia at 2007-7-12 3:06:18 > top of Java-index,Java Essentials,New To Java...
# 6

> how we have to mention the particular main() method

> to the java runtime system ?

By naming the class whose main you wish to call when you start the JVM. Example:

java HelloWorld

...will call the HelloWorld class' main() method, provided it has the appropriate signature as mentioned above.

~

yawmarka at 2007-7-12 3:06:18 > top of Java-index,Java Essentials,New To Java...