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
How do you declare a string variable?
Can a java program have 2 main methods?
Does sprintf add a null terminator?
What is the difference between the boolean & operator and the && operator in java programming?
What is character in data type?
Why is the singleton pattern considered to be an anti pattern?
What is the difference between an interface and an abstract class?
What is the purpose of static keyword in java?
What is instanceof keyword?
State some advantages of java?
What is valid keyword in java?
How to find the given number is a prime number or not by getting input from the user
How do you format in java?
How to invoke external process in java.
How are destructors defined in java?