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 are different data structures in java?

528


How do I get 64 bit java?

525


Describe what a thread-local variable is in java?

565


What are triggers in DB? Explain their types. How do they work?

621


How does finally block differ from finalize() method?

583






Define an enumeration?

611


What is difference between throw and throws ?

596


What is externalizable interface?

578


What is 32 bit float?

559


Explain reverse a linked list iterative solution in java?

496


What is the use of optional ?

562


When to use runnable interface vs thread class in java?

514


Is it possible to use Semaphore/ Mutex in an Interrupt Handler?

532


Is there any difference between synchronized methods and synchronized statements?

582


How use .contains in java?

617