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");
}
}

Questions by kamal.sharma   answers by kamal.sharma

Showing Answers 1 - 45 of 45 Answers

Raghavendiran

  • 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..

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

  Was this answer useful?  Yes

Prabhat807

  • Aug 20th, 2011
 

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...

  Was this answer useful?  Yes

Suvind Kumar

  • 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.  

  Was this answer useful?  Yes

Pravin Kumar

  • 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.  

  Was this answer useful?  Yes

SHIVA

  • Sep 3rd, 2011
 

By the priority, level method will be executed.

  Was this answer useful?  Yes

Arpit_b

  • Sep 5th, 2011
 

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

  Was this answer useful?  Yes

venkat

  • 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. }

  Was this answer useful?  Yes

teena hadpawata

  • Jun 11th, 2012
 

Hi friend will be the output

  Was this answer useful?  Yes

Rashmi Ahuja

  • 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

  Was this answer useful?  Yes

Manas Naik

  • 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

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions