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

Multiple 'public static void main()'s

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guy's.....
This is my first post to the group and am a novice just beginning to learn Java. So please overlook any silly points that I would make.
In the code below I have two 'public static void main()'s.... and the code complies fine.....
My question is how does the JVM know which main() method to start off from and why does'nt the compiler give me an error???
Thanks....
public class Test{
static int num=0;
public Test(){
System.out.println(num+"--from outer constructor");
}
public static void main(String args[]){
System.out.println("In the first main");
Test t = new Test();
Inner i=new Inner();
}
public static class Inner{
public Inner(){
System.out.println(num+"--from inner constructor");
}
public static void main(String args[]){
System.out.println("In the second main");
}
}
}
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class containing main is the controlling class. You can't have two main in the same program /same file. THis is the starting point for the Java interpreter.

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You can have as many class files defined in your file but only one class file needs to be public.And in each class file you can have a main method defined but when you type in
java publicclassname the main method of the public class gets executed.If you want the main method of the other class in your file to be executed then you need to type java otherclass.In your case it a static inner class with in a class.So you have two class files basically:
Test.class
Test$Inner.class
So if you want the main method of the enclosing class to be executed you just need to type in java Test and the output would be
In the first main
0--from outer constructor
0--from inner constructor
if you want the main method of the inner class to be executed then you need to type java Test$Inner which would result in
In the second main.
Hope this helps.
Surya
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepa:
A small correction...hope you don't mind..
"You cant have two mains ...same file."
This sentence should be
"You can't have two main in the same program /same file
with the same signature."
You could defenitely have two mains like this:
public static void main(String[] args){
// conventional main, method.
}
public static void main (Integer[] argv){
// overloaded main method, with different
// input argument type.
}
Check it out.
So, the answer to the original question:
1. Yes, we can have as many main methods in a class
as we want, but they should have different signatures.
2. In the original code above, the two main() methods
are in different classes. That is not same as the one
I said above. Hence the code compiles. Now, why you want
to do this is a different discussion altogether.
Hope this helps.
Regds.
- satya
 
They weren't very bright, but they were very, very big. Ad contrast:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic