• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A class with two mains

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a small piece of code I got from another mock exam site:
public class doublemain
{
int x;

public static void main(Integer args[])
{
System.out.println("in main method with int args");
}

public static void main(String args[])
{
System.out.println("In main method with string args");
}
}

It was asked whether the code will compile and if yes then which version of main() will run.
The answer to this is: Yes the code compiles and the method of main that will run is the main() function taking the string args irrespective of the order in which they are written.
I am a newbie to Java can someone explain then why does Java allow for overloading of the main method if it knows that it will always run the String version (or so I suppose ?)

Please guide me in this matter

Thnx in advance
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am a newbie to Java can someone explain then why does Java allow for overloading of the main method if it knows that it will always run the String version (or so I suppose ?)



The JVM will only run the main() method that takes a particular signature (string array), but a main() method is just a method. It could be called by any class. So... Java allows it for the same reason it allows any other method, it may be called (just not by the JVM).

Henry
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shouldn't the answer be "yes it will compile, and whichever one is called is the one that will run"?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the usual unstated context for these questions is that you are attempting to compile and run the class shown, not some other class which calls it indirectly. So the question should be interpreted as, if we compile doublemain.java and then execute "java doublemain" (with appropriate classpath etc.), which method would be called? Which is what Henry was answering. There are other ways to invoke these methods more indirectly, but those are outside the standard assumed context for exam questions. Meaning, if the authors want you to consider possibilities like "what if another class calls the main() method?", then the question will be phrased in a way to make that possibility explicit. If they don't, then just assume that we're compiling and running the class shown, and nothing else.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method called main is only special in the way that if there is a "public static void main (String[])", then that class can be the starting point of an application and that particular method will be invoked.

In all other aspects main is a method like any other, so it can be overloaded, inherited, overridden etc. at will.
 
Prahalad Deshpande
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. Henry I thank you immensely. You solved one of my biggest confusions.. Although there are a lot other more
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
A method called main is only special in the way that if there is a "public static void main (String[])"



ALMOST TRUE
check

public static void main (String...)
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prahalad FYI,
The other valid main methods are
  • public static void main(String args[]){}
  • public static void main(String []args){}
  • public static void main(String... args){}
  • final public static void main(String args[]){}
  • strictfp public static void main(String args[]){}
  • final strictfp public static void main(String args[]){}


  • even you can change the name of args[] to prahalad[]...

    Again relook at the last 3 main methods...

    Can any body list some more valid main methods?
    [ November 09, 2006: Message edited by: Sanjeev Kumar Singh ]
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic