Can a Java class have more than main methods?

Can a Java class have more than one main methods?
The answer is YES.

A class in Java can have more than one method, as long as the other main method has a different signature. See the example below.
       


public class Main {

     public Main() {

     }

     public static void main(String[] args) {

             System.out.println("Main method, the entry point of the program!");

          int a = main(1);

             int b = main();

             new Main().Main();
     }


     public static int main(int a) {
             System.out.println("Other main method, returning an integer!");
             return 1;
     }


     public static int main( ) {  
            System.out.println("Other main method without parameter, returning an integer!");
         return 2;
     }
}



Comments

Popular posts from this blog

Can I name a method with the same name of the class in Java?

What is new in Java 8

Quicksort Java implementation