• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public static void main(String args)

 
Dep Joy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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!
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
It sounds like overloading, I might get it, thanks!
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is overloading.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic