Open In App

Valid variants of main() in Java

We know that a Java code begins to execute from the main method. During runtime, if JVM can’t find any main method then we will get a runtime exception: 
No such Method Error
 

Main method not found in class, please define the main method as:
public static void main(String[] args) 

to avoid this problem there should be the main method. We also know that the java main method has a particular prototype, which looks like: 
 



public static void main(String[] args)

 
Even though the above syntax(prototype) is very strict but some little changes are acceptable. This makes it not so strict that if we perform any change then we will get a runtime exception. We can do several allowed modification to our main method. 
The following Changes are acceptable. 
Let’s understand the different variants of main() that are valid.
 






class Test
{
    public static void main(String[] args)
    {
        System.out.println("Main Method");
    }
}

Output:

Main Method

Meaning of the main Syntax: 

public:  JVM can execute the method from anywhere.
static:  Main method can be called without object.
void:    The main method doesn't return anything.
main():  Name configured in the JVM.
String[]: Accepts the command line arguments.
args:- the name of the String array is args.
 




//Java code to understand that The Order of Modifiers don't matters
class Test
{
    static public void main(String[] args)
    {
        System.out.println("Main Method");
    }
}

Output:

Main Method




class Test
{
    public static void main(String[] args)
    {
        System.out.println("Main Method");
    }
}

Output:

Main Method




class Test
{
    public static void main(String []args)
    {
        System.out.println("Main Method");
    }
}

Output:

Main Method




class Test
{
    public static void main(String args[])
    {
        System.out.println("Main Method");
    }
}

Output:

Main Method




class Gfg{
 
       public static void main(String[] geeksforgeeks){
               System.out.println("Instead of args we have written geeksforgeeks");
       }
}

Output: 

Instead of args we have written geeksforgeeks




//Java code-> using Var-Args instead of the array
//please note these code may not run in gfg IDE, better run it on other IDEs e.g, eclipse
class Gfg{
     
        final public static void main(String... args){
        System.out.println("Var-args main method");
    }
}

Output: 

Var-args main method




class Test
{
    public static void main(final String[] args)
    {
        System.out.println("Main Method");
    }
}

Output:

Main Method




//Java code having the final main method
////please note these code may not run in gfg IDE, better run it on other IDEs e.g, eclipse
class Gfg{
  
    final public static void main(String[] args){
           
        System.out.println("final main method");
 
        }
}

Output: 

final main method




//Java code having Synchronized main method
//please note these code may not run in gfg IDE, better run it on other IDEs e.g, eclipse
class Test
{
    public synchronized static void main(String[] args)
    {
        System.out.println("Main Method");
    }
}

Output:

Main Method




//Java code-> using strictfp modifier in main method
//please note these code may not run in gfg IDE, better run it on other IDEs e.g, eclipse
class Test
{
    public strictfp static void main(String[] args)
    {
        System.out.println("Main Method");
    }
}

Output:

Main Method




class Test
{
    public static void main(String[] args)
    {
        System.out.println("Main Method String Array");
    }
    public static void main(int[] args)
    {
        System.out.println("Main Method int Array");
    }
}

Output:

Main Method String Array




class A
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Parent");
    }
}
 
class B extends A
{
 
}

Two class files, A.class and B.class are generated by a compiler. When we execute any of the two .class, JVM executes with no error. 

O/P: Java A
Main Method Parent
O/P: Java B
Main Method Parent




class A
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Parent");
    }
}
class B extends A
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Child");
    }
}

Two classes, A.class and B.class are generated by Java Compiler javac. When we execute both the .class, JVM executes with no error.

O/P: Java A
Main Method Parent
O/P: Java B
Main Method Child


Article Tags :