• 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

public static void main(String args)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we all know, the entry of a Java class is usually defined as
public static void main(String[] args)
My question is, why the compiler doesn't complain when I defined it as the topic of this message, that is:
public static void main(String args)
Provided a function expects a String array and we give it a String method, it will not compile.
public class Test
{
public static void main(String[] args)
{
CallAFunction("test"); //it won't compile
}
private void CallAFunction(String[] paras)
{
//do sth. here
}
}

Could anybody tell me why, thank you very much!
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can legally declare as many main() methods as you want, each with different argument lists, but only the one with (String[]) will be called when you invoke the class from the command line. If it isn't there, you'll get this runtime exception:
Exception in thread "main" java.lang.NoSuchMethodError: main
 
Dep Joy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like overloading, I might get it, thanks!
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is overloading.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic