• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java class with multiple main()s  RSS feed

 
Jagdish Kala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a class have multiple main() methods? if so, is there any order in which they be executed?
This question was asked in an interview.
If someone knows the answer, please provide it.
Thanks in advance,
--jk
 
Aneesha Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jagdish,
Yes a class can have multiple main methods, i.e multiple methods with the name "main". Of course, like all methods in Java, no 2 methods can have the same argument list ... so we can have multiple overloaded main methods in java.
There is no specific order in which these main methods will be executed. It will depend on how they are called. But as for 'THE' main method (public static void main(String args[])), it will be called as soon as the class is executed from the commandline as its the method that the JVM looks for and expects to find.
Hope that helps!
Cheers!
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concerning the format of the main method - although public static void main(String args[]) as stated above is the traditional format it is not obligatory. See http://www.coderanch.com/t/395059/java/java/public-static-void-main-string
 
Jagdish Kala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Aneesha. That was a good explanation. Thank you Carlos for the useful link.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!