Can we declare multiple main() methods in multiple classes.ie can we have each main method in its class in our program?

Showing Answers 1 - 4 of 4 Answers

Ravindra

  • Feb 4th, 2006
 

yes, v can implement main() method in each class. it's like a simple static method.we can call this method in another class only with the class name in which it is implemented.it helps the JVM to run the class.it is the first method executed by the JVM to execute the class.for example:---package main_method;class m1{ public static void main(String s[]){ System.out.println("Ravindra is a good boy");}}now the second class which also has the mai9n method.package main_method;class m2{public static void main(String s[]){ m1 mm=new m1();// making an object of the above class //now calling the main method of m1String ss=new String[10];m1.main(ss);// by class name becoz it is the static methodmm.main(ss);// by the object of the m1 class }}

  Was this answer useful?  Yes

arpit Agarwal

  • Jun 20th, 2007
 

Hi

   This Code Can Explain The Problem Better
       
      public class test {
 
 
 public static void main (String ar[])
 {
  System.out.println("First");
  test1 test = new test1();
  String x[]={"!","1"};
      test1.main(x);
 }

}
class test1
{
 public static void main (String ar[])
 {
  System.out.println("Second");
 }
 
}

This Will Execute
Out put is
   First
  Second

  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