Valid variants of main() in Java
Below are different variants of main() that are valid.
- Default prototype: Below is the most common way to write main() in Java.
classTest{publicstaticvoidmain(String[] args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
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.
- Order of Modifiers: We can swap positions of static and public in main().
classTest{staticpublicvoidmain(String[] args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- Variants of String array arguments: We can place square brackets at different positions and we can use varargs (…) for string parameter.
classTest{publicstaticvoidmain(String[] args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
classTest{publicstaticvoidmain(String []args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
classTest{publicstaticvoidmain(String args[]){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
classTest{publicstaticvoidmain(String...args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- Final Modifier String argument: We can make String args[] as final.
classTest{publicstaticvoidmain(finalString[] args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- Final Modifier to static main method: We can make main() as final.
classTest{publicfinalstaticvoidmain(String[] args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- synchronized keyword to static main method: We can make main() synchronized.
classTest{publicsynchronizedstaticvoidmain(String[] args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- strictfp keyword to static main method: strictfp can be used to restrict floating point calculations.
classTest{publicstrictfpstaticvoidmain(String[] args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- Combinations of all above keyword to static main method:
classTest{finalstaticsynchronizedstrictfpstaticvoidmain(String[] args){System.out.println("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- Overloading Main method: We can overload main() with different types of parameters.
classTest{publicstaticvoidmain(String[] args){System.out.println("Main Method String Array");}publicstaticvoidmain(int[] args){System.out.println("Main Method int Array");}}chevron_rightfilter_noneOutput:
Main Method String Array
- Inheritance of Main method: JVM Executes the main() without any errors.
classA{publicstaticvoidmain(String[] args){System.out.println("Main Method Parent");}}classBextendsA{}chevron_rightfilter_noneTwo 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. (See Shadowing of static functions for details.)
classA{publicstaticvoidmain(String[] args){System.out.println("Main Method Parent");}}classBextendsA{publicstaticvoidmain(String[] args){System.out.println("Main Method Child");}}chevron_rightfilter_noneTwo 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
Recommended Posts:
- Java.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java

