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

Distinguish between a predicate and a function?

576


Why is a string immutable?

548


What's the difference between comparison done by equals method and == operator?

548


Explain about the main() method in java?

571


Which is bigger float or double?

522






How hashset works internally in java?

589


What is data type example?

556


What is the effect of keeping a constructor private?

477


What is an empirical question?

545


Is java call by value?

588


What is an example of a keyword?

550


Can we override private methods?

549


Write a program to print fibonacci series

617


Is java 1.7 the same as java 7?

540


String and stringbuffer both represent string objects. Can we compare string and stringbuffer in java?

558