Valid Variations of main() in Java


Main method is launcher method acts as an entry point for the JVM to start execution of a program. JVM always looks the main() method signature to launch the program. Below are different variation of main() method that are valid in Java.

  1. Default Prototype: This most  preferred way to write main() method in Java
    class TestClass
    {
        public static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    

    Meaning of the main Syntax:

    • main(): This is launcher method configured in the JVM to initiate execution of a program.
    • String[]: These are parameters passed as command line arguments.
    • public: This keyword is access modifier to define scope of a method. For JVM can execute the method from anywhere.
    • static: This keyword is Non access modifier use to show part of a class.Here use with Main method so that called by JVM without any object.
    • void: The main method doesn’t return anything. void keyword use to as part of method signature if there is no return type.
  2. Order of Modifiers: We can swap position of modifiers (static and public) in main method.
    class TestClass
    {
        static public void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  3. Variants of String Array Arguments: We can place square brackets at different positions or use varargs (…) for arguments in main method.
             Arguments Array Declaration (Way 1):

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

    Arguments Array Declaration (Way 2):

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

    Arguments with Variants:

    class TestClass
    {
    	public static void main(String...args)
    	{
    		System.out.println("Main Method");
    	}
    }
    
  4. Final Modifier to static main method: We can make main() as final.
    class Testclass
    {
        public final static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  5. Final Modifier String argument: We can make String args[] as final.
    class TestClass
    {
        public static void main(final String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  6. synchronized keyword to static main method:
    class TestClass
    {
        public synchronized static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  7. strictfp keyword to static main method: strictfp used to restrict floating point calculations.
    class TestClass
    {
        public strictfp static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  8. Combined all  above keyword to static main method:
    class TestClass
    {
        final static synchronized strictfp static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    

Inheritance of main() method

In inheritance JVM Executes the main() without any errors.

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

class Child extends Parent
{ 

}

or

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

In both the cases, Parent.class and Child.class files generated by Java compiler javac. When we execute any of the two .class, JVM will execute without any error as Child class hide parent class method when override.

Method Overloading with main()

Java allow the main method overloading but the program doesn’t execute the overloaded main method when we run your program. Program execution start for one method only which is following one of any above allowed main() method signatures.

import java.io.*;
public class Test { 

	// Normal main()
	public static void main(String[] args) {
		System.out.println("Hello Facing Issues on IT? (from main)");
		Test.main("Facing Issues on IT");
	} 

	// Overloaded main methods
	public static void main(String arg1) {
		System.out.println("Hello, " + arg1);
		Test.main("Dear ","Facing Issues on IT");
	}
	public static void main(String arg1, String arg2) {
		System.out.println("Hello , " + arg1 + ", " + arg2);
	}
}

Output


Hello Facing Issues on IT? (from main)
Hello  Facing Issues on IT
Hello Dear Facing Issues on IT
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s