Javatpoint Logo
Javatpoint Logo

Valid variants of main() in Java

The main method is the starting point for executing Java code. A runtime exception will be thrown if the JVM cannot locate a main method during runtime. In other words, if no main method is present in the Java code, the JVM will report an error at runtime.

  • The public access modifier in the main() method enables the JVM to execute the method from anywhere in the program.
  • The static keyword in the main() method declaration allows the method to be called without creating an object of the class.
  • The void return type specifies that the main() method does not return any value.
  • The name of the main() method is configured by the JVM, and it must always be named main().
  • The String[] parameter in the main() method allows the program to accept command-line arguments passed to the program when executed.
  • The name of the String array parameter is typically args, which refers to the array of String objects containing the command-line arguments.

Although the syntax of Java's main() method is generally strict, some minor modifications are allowed. These modifications do not result in a runtime exception. As a result, there are various valid variants of the main() method in Java. Understanding these variants is essential as they allow developers to modify the standard syntax slightly to suit their specific needs.

Default prototype

The following is the standard and widely accepted way to write Java's main() method.

Filename: Computer.java

Output:

Welcome

Order of Modifiers

The static and public modifiers in the main() method declaration can be interchanged without affecting the program's behavior.

Filename: Computer.java

Output:

Welcome

Variants of String array arguments

In Java, it is possible to place the square brackets at different positions when declaring the String parameter in the main() method.

Filename: Computer.java

Output:

Welcome

Filename: Computer.java

Output:

Welcome

Filename: Computer.java

Output:

Welcome

Args or anything

In the main() method in Java, you can use any valid Java identifier instead of args, such as your name, company name, or any other word or phrase that follows the rules for Java identifiers.

Filename: Computer.java

Output:

Welcome

Var-args instead of String array

In Java, the main() method can be declared with a variable-length argument parameter (varargs) instead of a String array. It provides more flexibility in handling command-line arguments.

In Java, if the String parameter in the main() method is a one-dimensional array, you can use a varargs parameter (denoted by three dots ... instead of square brackets []) as a replacement.

Filename: MainMethodDemo.java

Output:

Number of arguments: 0

The final Modifier String argument

In Java, it is possible to declare the String[] args parameter in the main() method as final, ensuring that the parameter's value cannot be changed.

Filename: MainMethodDemo.java

Output:

Arguments:

Final main method

In Java, the final keyword can be applied to the main() method to prevent the method from being overridden in any subclass. It is possible to declare the main() method with the final keyword, which does not affect the execution of the program or cause any errors.

Filename: DemoExample.java

Output:

It is a final main method

Synchronized Keyword to Static Main Method

It is possible to declare the main() method synchronized in Java.

Filename: ComputerDemo.java

Output:

Synchronized Main Method

strictfp keyword to the static main method

In Java, the strictfp keyword ensures consistent floating-point arithmetic across different platforms.

Filename: DemoStrictfp.java

Output:

x + y = 1.0

Overloading the Main method

We can overload the main() method in Java like any other method. Overloading means creating multiple methods with the same name but with different parameters. We can have any number of overloaded main() methods, but the one with the signature public static void main(String[] args) will be the entry point of the program.

Filename: MainOverloading.java

Output:

It is the main() method with String[] argument

Inheritance of the Main method

In Java, you can inherit the main() method from a superclass to a subclass as long as the method signature remains the same. If the subclass does not define its own main() method, it will inherit the main() method of its superclass. If the subclass does define its own main() method, it will override the main() method of its superclass. JVM Executes the main() without any errors.

Filename: Animal.java

Output:

It is the main method of the Animal class

Method Hiding of main(), but not Overriding

In Java, it's possible to "hide" the main method of a parent class in a child class using method hiding, but it's impossible to override it since it's a static method. Here's an example of a method hiding of main():

Filename: Animal.java

Output:

It is the main method of the Animal class






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA