Valid variants of main() in Java
Last Updated :
11 May, 2022
We know that a Java code begins to execute from the main method. During runtime, if JVM can't find any main method then we will get a runtime exception:
No such Method Error:
Main method not found in class, please define the main method as:
public static void main(String[] args)
to avoid this problem there should be the main method. We also know that the java main method has a particular prototype, which looks like:
public static void main(String[] args)
Even though the above syntax(prototype) is very strict but some little changes are acceptable. This makes it not so strict that if we perform any change then we will get a runtime exception. We can do several allowed modification to our main method.
The following Changes are acceptable.
Let's understand the different variants of main() that are valid.
- Default prototype: Below is the most common way to write main() in Java.
Java
class Test
{
public static void main(String[] args)
{
System.out.println("Main Method");
}
}
Output:
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.
args:- the name of the String array is args.
- Order of Modifiers: We can swap positions of static and public in main().
Java
//Java code to understand that The Order of Modifiers don't matters
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 for string parameter.
Java
class Test
{
public static void main(String[] args)
{
System.out.println("Main Method");
}
}
Output:
Main Method
Java
class Test
{
public static void main(String []args)
{
System.out.println("Main Method");
}
}
Output:
Main Method
Java
class Test
{
public static void main(String args[])
{
System.out.println("Main Method");
}
}
Output:
Main Method
- Args or anything: Instead of args we can write anything which is a valid java identifier. You can write anything here, you can write your name or company's name or anything you want to write but it must follow the rule of being a java identifier.
Example:
Java
class Gfg{
public static void main(String[] geeksforgeeks){
System.out.println("Instead of args we have written geeksforgeeks");
}
}
Output:
Instead of args we have written geeksforgeeks
- Var-args instead of String array: According to the rule whenever there is one dimensional array we can replace the array with var-arg parameter.So here we can change our string array using var-args. (the triple dots instead of [])
Example:
Java
//Java code-> using Var-Args instead of the array
//please note these code may not run in gfg IDE, better run it on other IDEs e.g, eclipse
class Gfg{
final public static void main(String... args){
System.out.println("Var-args main method");
}
}
Output:
Var-args main method
- Final Modifier String argument: We can make String args[] as final.
Java
class Test
{
public static void main(final String[] args)
{
System.out.println("Main Method");
}
}
Output:
Main Method
- Final main method: We can declare the main method with the final keyword.This cannot change the execution or give any error.
Example:
Java
//Java code having the final main method
////please note these code may not run in gfg IDE, better run it on other IDEs e.g, eclipse
class Gfg{
final public static void main(String[] args){
System.out.println("final main method");
}
}
Output:
final main method
- synchronized keyword to static main method: We can make main() synchronized.
Java
//Java code having Synchronized main method
//please note these code may not run in gfg IDE, better run it on other IDEs e.g, eclipse
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.
Java
//Java code-> using strictfp modifier in main method
//please note these code may not run in gfg IDE, better run it on other IDEs e.g, eclipse
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:
So we can declare the java main method with the following modifiers: - Overloading Main method: We can overload main() with different types of parameters.
Java
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.
Java
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 a 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.)
Java
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
Similar Reads
Java Methods
Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
8 min read
Parameter Passing Techniques in Java with Examples
There are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the "caller function" and B is called the "called function or callee function". Also, the arguments wh
4 min read
Java is Strictly Pass by Value!
In order to understand more of how the java is processing the parameter in methods and functions, lets compare the java program with a C++ code which would make it more clear and helps you get the major difference between how the parameters are being passed to any methods or functions wrt passing pa
6 min read
How are parameters passed in Java?
See this for detailed description. In Java, parameters are always passed by value. For example, following program prints i = 10, j = 20. java // Test.java public class Test { // swap() doesn't swap i and j public static void swap(Integer i, Integer j) { Integer temp = new Integer(i); i = j; j = temp
1 min read
Method overloading and null error in Java
In Java it is very common to overload methods. Below is an interesting Java program. Java public class Test { // Overloaded methods public void fun(Integer i) { System.out.println("fun(Integer ) "); } public void fun(String name) { System.out.println("fun(String ) &quo
2 min read
Can we Overload or Override static methods in java ?
Let us first define Overloading and Overriding. Overriding: Overriding is a feature of OOP languages like Java that is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in the superclass (or base class). The implementation to be executed i
5 min read
Access specifier of methods in interfaces
In Java, all methods in an interface are public even if we do not specify public with method names. Also, data fields are public static final even if we do not mention it with fields names. Therefore, data fields must be initialized. Consider the following example, x is by default public static fina
1 min read
Java main() Method - public static void main(String[] args)
Java's main() method is the starting point from where the JVM starts the execution of a Java program. JVM will not execute the code if the program is missing the main method. Hence, it is one of the most important methods of Java, and having a proper understanding of it is very important.The Java co
6 min read
Is main method compulsory in Java?
The answer to this question depends on the version of java you are using. Prior to JDK 7, the main method was not mandatory in a java program. You could write your full code under static block and it ran normally. The static block is first executed as soon as the class is loaded before the main(); t
2 min read
Understanding "static" in "public static void main" in Java
Following points explain what is "static" in the main() method: main() method: The main() method, in Java, is the entry point for the JVM(Java Virtual Machine) into the java program. JVM launches the java program by invoking the main() method. Static is a keyword. The role of adding static before an
3 min read