Tutorialspoint.dev

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.
    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. 
  2. Order of Modifiers: We can swap positions of static and public in main().
    class Test
    {
        static public void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }

    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.
    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 Test
    {
        public static void main(String...args)
        {
            System.out.println("Main Method");
        }
    }

    Output:

    Main Method
  4. Final Modifier String argument: We can make String args[] as final.
    class Test
    {
        public static void main(final String[] args)
        {
            System.out.println("Main Method");
        }
    }

    Output:

    Main Method
  5. Final Modifier to static main method: We can make main() as final.
    class Test
    {
        public final static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }

    Output:

    Main Method
  6. synchronized keyword to static main method: We can make main() synchronized.
    class Test
    {
        public synchronized static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }

    Output:

    Main Method
  7. strictfp keyword to static main method: strictfp can be used to restrict floating point calculations.
    class Test
    {
        public strictfp static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }

    Output:

    Main Method
  8. Combinations of all above keyword to static main method:
    class Test
    {
        final static synchronized strictfp static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }

    Output:

    Main Method
  9. Overloading Main method: We can overload main() with different types of parameters.
    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
  10. Inheritance of Main method: JVM Executes the main() without any errors.
    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 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.)
    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


This article is attributed to GeeksforGeeks.org

tags:

Java Java

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter