is possible to overload a main method? If yes from which method the jvm will start executing?
7 Answers
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);

- 58,763
- 21
- 123
- 144
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

- 11,813
- 16
- 59
- 70
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.

- 53,625
- 36
- 139
- 164

- 26,773
- 63
- 143
- 176
-
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 Jan 01 '10 at 08:05
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)

- 39,895
- 28
- 133
- 186
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.

- 351
- 4
- 10
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.

- 11,215
- 5
- 45
- 42
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.

- 9,339
- 6
- 34
- 51
-
Yes I was banned a year ago on mea for asking one off topic question which had a severe impact on my experience here as I was essentially cut off from the main site to ask questions with relation to correct use of SO and sister sites. I raised the issue by contacting them and nothing has happened. Despite that I was undergoing chemo at the time and recovering from major brain surgeries. – thejartender Jun 11 '12 at 14:33
-
Ultimately chemo/brain surgery, while a PITA, isn't relevant to SO content--which is what the moderators and devs are focused on (which is reasonable). All you can do is try again, which I'd suggest you do--the worst that can happen is nothing. – Dave Newton Jun 11 '12 at 14:36
-
And in addition the reponse I get is "improve my questions" How does one improve deleted questions? – thejartender Jun 11 '12 at 14:36
-
1By editing and voting to re-open, and/or flagging for moderator attention--same with answers. With regards to answers, though, I'd be hesitant to try too hard on the older, accepted-answer kinds like the last few we've discussed. I think those may be a lost cause, for a variety of reasons. – Dave Newton Jun 11 '12 at 14:37