Java Reference
In-Depth Information
Why do you need to declare the main() method as static ? The main() method serves as the entry point for a
Java application. It is invoked by the JVM when you run a class. The JVM does not know how to create an instance of
a class. It needs a standard way to start a Java application. Specifying all details about the main() method and making
it static provides the JVM a standard way to start a Java application. By making the main() method static , the JVM
can invoke it using the class name, which is passed on the command line.
What will happen if you do not declare the main() method as static ? If you do not declare the main() method as
static , it will be treated as an instance method. The code will compile fine. However, you will not be able to run the
class, which has its main() method declared as an instance method.
Can you have more than one main() method in a class? The answer is yes. You can have multiple methods in
a class, which can be named main as long as they do not have the same signature. The following declaration for the
MultipleMainMethod class, which declares three main() methods, is valid. The first main() method, which is declared
as public static void main(String[] args) , may be used as the entry point to the Test class. The other two
main() methods have no special significance as far as the JVM is concerned.
// MultipleMainMethod.java
package com.jdojo.cls;
public class MultipleMainMethods {
public static void main(String[] args) {
/* May be used as the application entry point */
}
public static void main(String[] args, int a) {
/* Another main() method */
}
public int main() {
/* Another main() method */
return 0;
}
}
Is it required for each class in Java to have a main() method? The answer is no. It is required that you declare
a public static void main(String[] args) method in a class if you want to run that class. If you have a Java
application, you will need to have a main() method in at least one class so you can start you application by running
that class. All other classes that are used in the application, but are not used to start the application, do not need to
have a main() method.
Can you invoke the main() method in your code? Or, can it be invoked only by the JVM? The main() method is
invoked by JVM when you run a class. Apart from that, you can treat the main() method as any other class method.
Programmers have a general (and wrong) impression that the main() method can only be invoked by a JVM.
However, that is not true. It is true that the main() method is generally (but not necessarily) invoked by a JVM to start
a Java application. However, it does not have to be invoked (at least theoretically) only by a JVM. Here is an example
that shows how the main() method can be invoked like any other class method. Listing 6-9 has the definition of a
MainTest1 class, which declares a main() method. Listing 6-10 has the definition of a MainTest2 class, which declares
a main() method.
Search WWH ::




Custom Search