GeekInterview.com
Series: Subject: Topic:
Question: 34 of 573

if there are more than one main method in a program, then which main method will be called.

what will be the output class A { public static void main(String args[]) { System.out.println("Hi friend"); } } class Test { public static void main(String args[]) { System.out.println(" How are you"); } }
Asked by: kamal.sharma | Member Since Jul-2011 | Asked on: Aug 5th, 2011

View all questions by kamal.sharma   View all answers by kamal.sharma

Showing Answers 1 - 15 of 15 Answers
Raghavendiran

Answered On : Aug 5th, 2011

Depends on which object u invoke on run time..
when these file containing these classes are compiled, (assuming u have a public class in that file), A.class and Test.class would be created..

then if you enter java A, then main inside A would be executed.. if you enter java Test, then main inside Test would be executed..

Yes  4 Users have rated as useful.
  
Login to rate this answer.

When any java file has more than one class and those classes if have different main() then the class which will be loaded by jvm, will be run and it's main method will be executes. And let you save your program by "a.java"

for your code


1. Javac a.java // will create a.class file and test.class file
now,

for output on console as hi friend... You need to write java a
i.e you have loaded a class and it's main() will execute, now same for test.class .. You need to write java test and it will result in how are you

  
Login to rate this answer.
ravitejareddy

Answered On : Aug 18th, 2011

View all answers by ravitejareddy

it depends on class which you call at run time....

  
Login to rate this answer.

it will give error.bcoz java is pure OOP language..so everything should in class...

  
Login to rate this answer.
Prabhat807

Answered On : Aug 20th, 2011

View all answers by Prabhat807

This peace of code does not give any error, it will decide in run time to which main method will be called based on the object called...

  
Login to rate this answer.
Suvind Kumar

Answered On : Aug 21st, 2011

It will give error because first u creating a class and after that closed the class and again u create a class with main method.This is invalid. it is possible to create double main method in a program but when the first class will be closed then it will give error at compile time...

Code
  1. class A{
  2. public static void main()
  3. {
  4. System.out.println("Hi Friends");
  5. }}
  6. class B extends A
  7. {
  8. public static void main(String [] ar)
  9. {
  10. main();
  11. System.out.println("How r u?");
  12. }
  13. B b=new B();
  14. }
  15.  

  
Login to rate this answer.
Pravin Kumar

Answered On : Aug 21st, 2011

only one main method is called ((public static void main(String []ar)) and remaining main method will be called by main method ... Ex....

Code
  1. class A{
  2. public static void main()
  3. {
  4. System.out.println("Hi Freiends");
  5. }}
  6. class B extends A
  7. {
  8. public static void main(String [] ar)
  9. {
  10. main();
  11. System.out.println("How r u?");
  12. }
  13. B b=new B();
  14. }
  15.  

  
Login to rate this answer.
ganga24473

Answered On : Aug 26th, 2011

View all answers by ganga24473

Class A will execute first

  
Login to rate this answer.
SHIVA

Answered On : Sep 3rd, 2011

By the priority, level method will be executed.

  
Login to rate this answer.
Arpit_b

Answered On : Sep 5th, 2011

View all answers by Arpit_b

It depends which class file main method you invoked.
because two separate class file generate as soon as it is compiled by the JVM.

  
Login to rate this answer.
venkat

Answered On : Sep 12th, 2011

output is hi friend

Code
  1. class A
  2. {
  3. public static void main(String args[])
  4. {
  5. System.out.println("hi friend");
  6. }
  7. }
  8.  
  9. class Test
  10. {
  11. public static void main(String args[])
  12. {
  13. System.out.println("how are you");
  14. }
  15. }

  
Login to rate this answer.
sampra

Answered On : Mar 13th, 2012

View all answers by sampra

At run time it will ask that which method do you want to run

  
Login to rate this answer.
teena hadpawata

Answered On : Jun 11th, 2012

Hi friend will be the output

  
Login to rate this answer.
Rashmi Ahuja

Answered On : Jun 16th, 2012

when we execute this class with class B then it first call to class A method main which is like normal method. after that B class print msg is printed

  
Login to rate this answer.
Manas Naik

Answered On : Jul 7th, 2012

When we compile it create two different class with name a.class and test.class .so,according to your call it will execute.
Example--java test
then output is "how are you" only not hi friend

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.