wood burning stoves 2.0*
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overloading main method" Watch "overloading main method" New topic
Author

overloading main method

s.palanivel rajan
Ranch Hand

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


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

Joined: Mar 22, 2005
Posts: 41634
    
  55
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.


Ping & DNS - my free Android networking tools app
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
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

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: 14117
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Malarvizhi Muthu
Greenhorn

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("Test");
main(4);
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
test 5
Main method example
s.palanivel rajan
Ranch Hand

Joined: Sep 22, 2009
Posts: 40
Thanks......

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
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

Malarvizhi, please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading main method