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 - 59 of 59 Answers

Asheesh Kumar Rajput

  • Jul 5th, 2005
 

YES

  Was this answer useful?  Yes

Srinivasa Reddy

  • Aug 4th, 2005
 

Yes  
But only one public class will present in that program 

  Was this answer useful?  Yes

suresh

  • Oct 13th, 2005
 

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

  Was this answer useful?  Yes

Pardeep Saini

  • Dec 12th, 2005
 

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.

  Was this answer useful?  Yes

Mithun

  • Feb 14th, 2006
 

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.

  Was this answer useful?  Yes

Anmiaka

  • Apr 2nd, 2007
 

Yeah,we can declare multiple main() in multiple classes,and can complile and  execute without any error,i tried its working.

  Was this answer useful?  Yes

naresh

  • Apr 6th, 2007
 

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

  Was this answer useful?  Yes

Aashi

  • May 8th, 2007
 

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

  Was this answer useful?  Yes

rameshvdnv

  • May 17th, 2007
 

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

Rony

  • Jun 7th, 2007
 

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.

  Was this answer useful?  Yes

kareem1201

  • Oct 3rd, 2007
 

Hai friend,

We can have a main method in each class of an  application.
Application is a set of classes and interfaces. In that application we usually have one class containing of a main method [pub static void main(String[] s) ] which is the stating point of the application for  the jvm.

But we can also have all the classes in the application containg the main method in each class. it would n't give any compilation error when we compile the application.
And even would not give any run time error also.

Why because, while compile the application we will give the application name to the compiler as an input. like        javac  applicationname.java
Then the compiler only check the systactical and semantical errors while its compilation process.

But where as while  Running the application, we will give the class name to the interpreter(jvm) as an input. As and when the jvm starts, it will first try to find the main method (with String array args) to run the application,that( main method) shoud be available before any object is created.

So we may have any number of classes in our application containing static main methods,but which class name we give to jvm, that class's main method will be considered as an entry point of the application by the jvm.

But we should not have two main methods in one class with the same type of arguments.And also we can overload the main method but there is no use of overloding it.why because the overloaded main method will never be called. jvm always call the main method with String  arry argument i.e, main(String[] s).

  Was this answer useful?  Yes

vijay

  • Oct 10th, 2007
 

no, you can't have multiple main methods in single source code...

  Was this answer useful?  Yes

sasi

  • Oct 27th, 2007
 

yes, we declare multiple main() methods.

  Was this answer useful?  Yes

padmanabham

  • Nov 13th, 2007
 

No , we can;t not declare multiple main methods in a class

if you write it throws

NoSuchMethodError

Exception in thread "main"

  Was this answer useful?  Yes

yes, we can write multiple main methods in different classes. we can compile both classes without error.but  while running jvm recognize only one main method which class we have mentioned in running. 

  Was this answer useful?  Yes

Yes, you can have multiple main() methods in multiple classes. but if gives only single main method's output depends upon ur saving settings.

Ex::
class Maintest{
public static void main(String args[]){
System.out.println("one");
}
 }
class Mt{
public static void main(String args[]){
System.out.println("two");}
 }
public class Ma{
public static void main(String args[]){
System.out.println("thr");}
 }


If you save with Maintest.java you will get "one" as output. if you save with Mt.java you will get "two" as output. like this it depends on your saving settings.If there is class contains Public as classname ,that must be saved as your classname.
Any how only one method must be executed.

  Was this answer useful?  Yes

sampra

  • Feb 11th, 2008
 

Yes we can use  it will compile sucessfully but during runtim we have to call by individual class name

  Was this answer useful?  Yes

yes it's legal and possible to have multiple main method in multiple classes...... but that one would be invoked which the public/default class has been named......

if need more explanation....... tell me i can give a small example

  Was this answer useful?  Yes

yes this is possible...... but within a single file only one class can be declared puiblic!!!!
and whichever class you run, that class's main() mentod would be called.......

vinny

  Was this answer useful?  Yes

Yes I tested this we can have main method in two different classes this will not give error and we should save that doc with the class name which has the main we want to be executed first and inside that main method we should create obj for another class which has another main method and we can access that in this way.

class test1 save as test1.java
{
public static void main(String[] as)
{
test2 obj=new test2();
String[] a={"first","second"};
obj.main(a);
}
}
class test2
{
public static void main(String[] as)
{
System.out.println("hai this is from another main");
}
} //output is hai this is from another main

and  another case is can we have one class that contains two mains and the answer is yes we can have two main methods in one class but their arg list must be different that is it is just like overloading.

  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