Different forms of main() method
Different writing style of main() method -
Valid forms of main() in Java
Below are different variants of main() that are valid.
- Default form: 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 MethodMeaning 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.
- Order of Modifiers: We can change order positions of static and public in main() method.
class Test{ static public void main(String[] args) { System.out.println("Main Method"); } }
Output:
Main Method
- Variants of String array arguments: We can place square brackets at different positions and we can use var args (…) 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- Method Hiding of main(), but not Overriding: Since main() is static, derived class main() hides the base class main.
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
post a comment