tags:

views:

207

answers:

5

is possible to overload a main method? If yes from which method the jvm will start executing?

+1  A: 

Yes. You can overload the main method, but the method below will be execute when you execute the class :

public static void main(String[] args)
fastcodejava
+4  A: 

You can overload the main method, but the JVM would always start the main method with the following signature:

public static void main(String[] args);
notnoop
A: 

According to the Java Language Spec:

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/third_edition/html/execution.html (12.1.4)

So, only the public static void main(String[] args) of your overloads will be executed.

ZoogieZork
+2  A: 

Yes. The main method can be overloaded just like any other method in Java.

The usual declaration for main is

public static void main(String[] args) throws Exception;

When you launch a java application it looks for a static method with the name 'main', return type 'void' and a single argument of an array of Strings. ie what you throw is unimportant in resolving this method.

Overloading is providing multiple methods with the same name but different arguments (and potentially return type).

With the above explaination we can overload the main method.

giri
The usual declaration for main is "public static void main(String[] args)" sans the throws Exception. Main should properly deal with exceptions rather than display a stack trace to the user!
TofuBeer
+3  A: 

As said by others, very much possible But, the execution will always start from

public static void main(String[] args)

A small program to demonstrate:

public class Test{

    public static void main(String [] args){
        System.out.println("First");
        main();
    }

    public static void main(){
        System.out.println("Second");   
    }
}

Output:

First
Second
gameover