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

What is internal variable?

733


Can private class be extended java?

758


Why is the main method static?

872


Explain the significance of class loaders in bootstrap?

705


Is null false in java?

926


What do you mean by formatting?

736


What is http client in java?

749


Does treeset use compareto?

738


how come we know the object is no more used in the class?

5966


What is array and arraylist in java?

761


What is a concrete classes? Is Java object class is concrete class?

740


What is bitwise complement?

718


What is collection class in java? List down its methods and interfaces.

721


What is a function easy definition?

734


How do you use wildcards?

741