top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can we use multiple main methods in java?

+3 votes
633 views
Can we use multiple main methods in java?
posted Jan 30, 2017 by Manisha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

In Java, you can have just one public static void main(String[] args) per class. Which mean, if your program has multiple classes, each class can have public static void main(String[] args) . See JLS for details. A class can define multiple methods with the name main.

Yes. While starting the application we mention the class name to be run. The JVM will look for the main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.

answer Feb 1, 2017 by Karthick.c
0 votes

Yes i can DO this.a java program can have more than one main() method as java supports overloading of main method also.

public class Test2 {
    public static void main (String[] args){

        System.out.println("I rule!");
                main(5);

        }
    public static void main(int i){
        System.out.print("Hi " + i);
    }
}
answer Jun 26, 2017 by Ajay Kumar
...