GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 50 of 502    Print  
Can we declare multiple main() methods in multiple classes.ie can we have each main method in its class in our program?

  
Total Answers and Comments: 23 Last Update: August 05, 2008   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: rameshvdnv
 
Yes, we can have multiple main()'s in a java application.And we can call a main() of one class to main()of another class.Here is the
Eg:
class Test{
 public static void main(String[] args)throws Exception{
for(int i=0;i<args.length;i++){
System.out.println(s);
}
}
class Arr{
  public static void main(String[] args)throws Exception{
 String s[]={"Xyz","lkm","abc"};
 Test.main(s);
}
}

Above answer was rated as good by the following members:
midhagaurav
  Sorting Options  
  Page 1 of 3   « First    1    2    3    >     Last »  
July 05, 2005 03:26:46   #1  
Asheesh Kumar Rajput        

RE: Can we declare multiple main() methods in multiple classes.ie can we have each main method in its cl...
YES
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
August 04, 2005 06:17:57   #2  
Srinivasa Reddy        

RE: Can we declare multiple main() methods in multiple classes.ie can we have each main method in its cl...
Yes
But only one public class will present in that program

 
Is this answer useful? Yes | No
October 13, 2005 05:41:44   #3  
suresh        

RE: can we declare multiple main() methods in multiple...
Yaa u can declare multiple main methods in different classes this wont through any error while complition but it through u runtime exception stating that no main method found.So there should be only one main method in a program
 
Is this answer useful? Yes | No
December 12, 2005 18:05:49   #4  
Pardeep Saini        

RE: can we declare multiple main() methods in multiple...
No. We can not declare multiple main() methods as main() method is associated with the static keyword. Whenever the compilation starts compiler looks for the keyword static for compiling the program as static keyword associates with the main() method. This is the reason we need not pass any object to call the main() function. If there will be multiple main() methods then it would be difficult for the compiler to distinguish between the functions i.e to start the compilation from which main() function should be compiled first.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
February 14, 2006 05:40:03   #5  
Mithun        

RE: can we declare multiple main() methods in multiple...
In a single application user can create multiple main methods in multiple classes. But while running the application user needs to give one class name (which is the enty point of the application) which must have to have a main method which will create the main thread to run your application.
 
Is this answer useful? Yes | No
April 02, 2007 07:14:56   #6  
Anmiaka        

RE: can we declare multiple main() methods in multiple...
Yeah we can declare multiple main() in multiple classes and can complile and execute without any error i tried its working.
 
Is this answer useful? Yes | No
April 06, 2007 13:14:49   #7  
naresh        

RE: can we declare multiple main() methods in multiple...
yes we can declare. but we can run only one at a time. whatever method we want to execute we hv to give command as c:/java <name> no matter of whwich name our file is saved
 
Is this answer useful? Yes | No
May 08, 2007 09:37:31   #8  
Aashi        

RE: can we declare multiple main() methods in multiple...
Yes we can declare multiple main method in various classes. When you compile it creates many class files. Run any class that has main method it will execute only that class. It works my friend
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
May 17, 2007 08:13:31   #9  
rameshvdnv Member Since: December 2006   Contribution: 12    

RE: can we declare multiple main() methods in multiple...
Yes we can have multiple main()'s in a java application.And we can call a main() of one class to main()of another class.Here is the
Eg:
class Test{
public static void main(String[] args)throws Exception{
for(int i 0;i<args.length;i++){
System.out.println(s);
}
}
class Arr{
public static void main(String[] args)throws Exception{
String s[] {"Xyz" "lkm" "abc"};
Test.main(s);
}
}

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 07, 2007 06:48:02   #10  
Rony        

RE: can we declare multiple main() methods in multiple...
We can declare main() methods in multiple classes but JVM will call only one main method and it doesn't throw any exception or error.
 
Is this answer useful? Yes | No
  Page 1 of 3   « First    1    2    3    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape