Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading main method

 
s.palanivel rajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we overload main()method.Give with example.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12181
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
 
Malarvizhi Muthu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Malarvizhi, please Use Code Tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic