• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

overloading main method

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we overload main()method.Give with example.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Malarvizhi, please Use Code Tags.
 
The only cure for that is hours of television radiation. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic