Skip to content
Related Articles

Related Articles

Can We Have Multiple Main Methods in Java?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 03 Aug, 2021
View Discussion
Improve Article
Save Article

Java is an object-oriented language all processing is carried within classes. Execution of a program means dictates java virtual machine to load the class and then start execution of its main method. Java’s main method is entry point of any Java program. Public access modifier is used before the main method so that JVM can identify the execution point of the program. If other access modifiers are used instead of public, it will not be visible to JVM.

Initially, JVM loads the class but there is no object of the class present to call the main method. That is why the main method has to be static so that JVM can load the class and call the main method without having object of class. Java main method does not return anything that is why its return type is void. If we try to return anything from main method, it will give an unexpected value error because it is predefined signature in JVM. Java’s main method accepts string array as an argument. It is also called a command-line argument and it can pass from the command line in the main method. Now let us implement the same via appending clean java programs.

Implementation:

Example 1

Java




// Java Program Illustrating Can we have Multiple main
// methods
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // Method inside main() method
    void test()
    {
 
        // Print statement whenever this method is called
        System.out.print("Inside class GFG");
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object class inside main() method
        GFG obj = new GFG();
 
        // Calling the class object inside main() method
        obj.test();
    }
}

Output

Inside class GFG

In the above program we are simply calling test() method by using class object from main() method. Now let us go onto depicting program having multiple main() methods. 

Example 2 

Java




// Java Program Illustrating Can we have
// Multiple main() Methods
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method  1
    void test()
    {
 
        // Print statement when this method is called
        System.out.print("inside test");
    }
 
    // Method 2
    // Main driver method
    public static void main(int i)
    {
 
        // Creating object later calling of class
        // inside this main() method
        GFG obj = new GFG();
        obj.test();
    }
 
    // Method 3
    // Main driver method
    public static void main()
    {
 
        // Creating object later calling of class
        // inside this main() method
        GFG obj = new GFG();
        obj.test();
    }
}

Output: 

Output explanation:

Above program consist of two main methods but throws out an error that the Main method is not found in class, please define the main method as public static void main(String[] args)”. Only the main() method with a single string array as a parameter is considered as an entry point of the program. JVM only looks for main method with string array as an argument. In order for other main methods to execute, you need to call them from inside  public static void main(String[ ] args)

Example 3  

Java




// Java Program Illustrating Can we have
// Multiple main() Methods
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // Main driver method
    public static void main(int i)
    {
 
        // Print statement for method 1
        System.out.println(i);
    }
 
    // Method 2
    // Main driver method
    public static void main(String s)
    {
 
        // Print statement for method 2
        System.out.println(s);
    }
 
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
 
        // Calling above 2 main methods
        main(1);
        main("hi");
    }
}

Output

1
hi

From the above program, we can say that Java can have multiple main methods but with the concept of overloading. There should be only one main method with parameter as string[ ] arg. 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!