Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RunTime error with main

 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My doubt is related to main methid. Already I saw "main method should be public static" , but if my intention is to give a method called main, so that other object can call this , any runtime error as quoted ??
class test1{
final static void main(String args[]){
System.out.println(" from test 1");
}
}
class test2{
final static void main(String args[]){
System.out.println(" from test 2");
}
}
class test3{
public static void main(String args[]){
test1.main(new String[]{"A"});
test2.main(new String[]{"B"});
}
}
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will not object to any declaration of a method named main as long as the declaration is a legal method declaration. However, the JVM places restrictions on a main method that is called from the command line. In your example, you are free to call test3.main from the command line, but you may not call test1.main or test2.main from the command line.
Of course, using the name "main" for a method that is not called from the command line could confuse readers.
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there
There is No runtime error there is no problem with this code neither at Runtime nor at compile time it runs smooooothly .
Although its a bit confusing at first look
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah... I was also telling there is no runtime exception when we use main not as command line methd. But the discussion was on main method called from command line . I asked about main methods not called from command line.
Ofcourse there is no runtime error in the code specified. Sorry if it mislead
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic