Is there is any error if you have multiple main methods in
the same class?

Answer Posted / rakesh nath

Well, I think we are going out of the question. The question
is that whether there is any error if we have multiple main
methods. Answer is not necessarily. I would like to change
the question in this way. Will it be possible to have
multiple main methods in the same class? Now the answer is
YES. WE CAN. But there should be only one main method that
has the complete signature "public static void main(String
args[])".

For example,
class Test
{
public static void main(String args[]){....}
public static void main(int args[]){....}
public static void main(boolean args[]){....}
public static void main(Object args[]){....}
}
this code will work without any error.

Remember there should be ONLY ONE main method with the
signature "public static void main(String <varargs>)"
because the compiler searches for this method to be loaded
into the bootstrap loader. If there are multiple methods
with this signature, it will throw an error.

Is This Answer Correct ?    42 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why do we need array in java?

520


What are the classes of java?

527


What is a key in java?

487


What is a vararg?

531


What is formatted output?

499






Explain constructors and types of constructors in java.

629


Can we pass a primitive type by reference in java? How

518


Describe the term diamond problem.

561


Can we initialize the final blank variable?

569


How to retrieve data from database in java using arraylist?

514


Is a boolean variable?

505


What does a boolean method return?

615


What is the primary benefit of encapsulation?

544


What is byte data type?

535


How do you start a new line in java?

505