File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes overloading main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overloading main method" Watch "overloading main method" New topic

overloading main method

s.palanivel rajan
Ranch Hand

Joined: Sep 22, 2009
Posts: 41
Can we overload main()method.Give with example.

With Regards,
S.Palanivel Rajan B.E.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Yes, that's possible - "main" is a method like any other from an OO point of view. Do you know what overloading means? If so, can you come up with an example of how that might work?

Of course, the JVM only knows what to do with the "public static void main (String[])" method; it won't do anything with any of the other methods.
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
yes. its possible .

Each method has a signature, which comprises the name of the method and the types and order of the parameters in the formal
parameter list. Several method implementations may have the same name, as long as the method signatures differ. Since overloaded methods have the same name, their parameter lists must be different.

so if your method is

public static void main(String args[]){


public static int main(int i){


both methods signature is different.

so its possible . try this example
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

The easiest way to find out would be to try it yourself. You'd probably learn more that way, too. Why not give it a shot??

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15093

This question is asked in the forums regularly. Is this a standard job interview or test question?

can a class or a java file have more than one "main "method?
overloading main()
Can we Overload the main() Method?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Malarvizhi Muthu

Joined: Jul 07, 2008
Posts: 4
Yes,We can overload main method in a java class. But in order to run a class the JVM will look for

public static void main(String[] args){

only. Otherwise it will show an error as "java.lang.NoSuchMethodError: main"

That is the execution will start only in this method. For example

public class MainExample {
public static void main(String[] args) {
MainExample me=new MainExample();
System.out.println(me.main("Main method example"));

public static void main(int k){
System.out.println("test 5");
String main(String s){
return s;

This class will compile and give the output as

test 5
Main method example
s.palanivel rajan
Ranch Hand

Joined: Sep 22, 2009
Posts: 41

Ulf Dittmer
swapnl patil
fred rosenberger
Jesper Young
Malarvizhi Muthu

for all your valuable descriptions... now i got the coreect idea...
thank you once again....
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Malarvizhi, please Use Code Tags.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: overloading main method
jQuery in Action, 3rd edition