Valid variants of main() in Java

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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 and we can use varargs (…) 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
    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. 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
  5. Final Modifier to static main method: We can make main() as final.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Main Method
  6. synchronized keyword to static main method: We can make main() synchronized.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Main Method
  7. 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

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

    chevron_right

    
    

    Output:

    Main Method
  8. Combinations of all above keyword to static main method:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Main Method
  9. 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
  10. 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 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
  11. 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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


5


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