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.

  1. Default prototype: Below is the most common way to write main() in Java.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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.
     
  2. Order of Modifiers: We can swap positions of static and public in main().
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    //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");
        }
    }

    chevron_right

    
    

    Output:

    Main Method
  3. Variants of String array arguments: We can place square brackets at different positions for string parameter.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Main Method
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Main Method
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Main Method
  4. Args or anything: Instead of args we can write anything which is a valid java identifier. You can write anything here, you can write your name or company’s name or anything you want to write but it must follow the rule of being a java identifier.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Instead of args we have written geeksforgeeks
  5. Var-args instead of String array: According to the rule whenever there is one dimenssional array we can replace the array with var-arg parameter.So here we can change our string array using var-args. (the tripple dots instead of [])
    Example:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    //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");
        }
    }

    chevron_right

    
    

    Output:

    Var-args main method
    
  6. Final Modifier String argument: We can make String args[] as final.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Main Method
  7. Final main method: We can declare the main method with the final keyword.This cannot change the execution or give any error.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    //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");
      
            }
    }

    chevron_right

    
    

    Output:

    final main method
    
  8. synchronized keyword to static main method: We can make main() synchronized.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    //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");
        }
    }

    chevron_right

    
    

    Output:

    Main Method
  9. strictfp keyword to static main method: strictfp can be used to restrict floating point calculations.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    //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");
        }
    }

    chevron_right

    
    

    Output:

    Main Method
  10. Combinations of all above keyword to static main method:
    So we can declare the java main method with the following modifiers:

      1.final,
      2.synchronized
      3.strictfp

    but wait, can we use these three modifiers altogether?
    -Yes, we can. we can use these three modifiers all together in our main method. Please go through the following example where we use all these modifiers and the code compiles and run perfectly fine without giving any error.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    final synchronized strictfp main method
  11. Overloading Main method: We can overload main() with different types of parameters.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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");
        }
    }

    chevron_right

    
    

    Output:

    Main Method String Array
  12. Inheritance of Main method: JVM Executes the main() without any errors.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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
  13. Method Hiding of main(), but not Overriding: Since main() is static, derived class main() hides the base class main. (See Shadowing of static functions for details.)
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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");
        }
    }

    chevron_right

    
    

    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

This article is contributed by Mahesh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Improved By : PinakiBanerjee0

Article Tags :
Practice Tags :


7


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.