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.
- 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.
- 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"); } }
- 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"); } }
- 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"); } }
- 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"); } }
- synchronized keyword to static main method:
class TestClass { public synchronized static void main(String[] args) { System.out.println("Main Method"); } }
- 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"); } }
- 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
You must log in to post a comment.