is possible to overload a main method?
is possible to overload a main method? If yes from which method the jvm will start executi开发者_高级运维ng?
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);
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
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.
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)
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.
A main method with String as its arguments is the default entry point into a program. You can Overload but it would not change the entry point of the program.
Yes you can. The jvm is smart enough to know which one to load as it looks at the method declaration that matches your main method and is logical. The parts of the main method declaration make perfect sense when you think like the 'jvm' and picture what the main method does (starts the application):
public
, because this method must be accessible by the jvm (not written by you).static
, implying this method can be accessed without having an object (because it's representation never changes), but here the logic is easily understood if you think like the jvm again; "I don't have any objects to create (instantiate) objects, so I need a static method to start the application as there simply isn't any logical way to get an instance specific method up yet as I don't have anything up yet to create objects".void
This method can't logically return anything because there is nothing up yet to return anything to. It is the start point of the application.main
I am the main method as without me you won't have an application.String[] args
Send me data you may feel useful for my start up.
精彩评论