Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The main() method is designated as static to allow the JVM to invoke it without needing to instantiate the class that encompasses the main() function. As there is no class object present upon the start of the Java runtime, it is necessary to declare the main() function as static.
The main() method in Java is the point from which the program begins its execution or simply the entry point of Java programs. In conclusion, it is one of the most crucial Java methods, and a thorough understanding of it is critical.
When a Java program is executed, the Java compiler or JVM looks for the main method. For the JVM to recognize the main method as its entry point, its signature must be in a specific format. If we change the method signature, the program compiles but does not run. In this blog, we will learn why the main method is static in java.
Reasons behind Defining the main() Method as Static
Here are some of the reasons for the main method in Java being static.
The Java Virtual Machine can call it without having to create an instance of the class that contains it. That's why the main method is static in Java.
Because C and C++ both have a similar main method that serves as the starting point for program execution, adhering to that convention will only benefit Java.
JVM would have to create an instance of the main Class, and because the function Object() [native code] can be overloaded and have arguments, there would be no certain and more consistent way for the JVM to find the main method in Java, that's why the main method is static in Java.
If the main method is not declared static, the JVM must create an instance of the main Class, and because the constructor can be overloaded and have arguments, there is no certain and consistent way for the JVM to find the main method in Java. Now we know why the main method is static in Java, what are the reasons behind it and also what each keyword means in the syntax while we write the main statement.
Divide main() Method syntax into Several Parts
Let's break down the main() method's syntax into its various components and examine each one separately.
Public
It is easy to comprehend. It is a main() method access modifier. We define the main() method with a public access specifier so that it can be executed by any program. As a result, we must make the main() method public, and if we make it non-public, the following error will occur:
public class CodingNinjasTest {
// define main() method as non-public
static void main(String[] args){
System.out.println("Non-public Main method.");
}
}
Output:
Static
The main() method in Java is static so that the JVM may call it without creating an instance of the class that contains the main() function. This allows the JVM to start the execution of the program without needing to instantiate any objects. By declaring main() as static, we allow the JVM to load the class into memory and call the main() method directly.
So, if we define the main() method as non-static, JVM will be unable to call it and will throw the following error:
public class CodingNinjasTest {
// do not define main() method as static
public void main(String[] args){
System.out.println("Non-static Main method.");
}
}
Output:
void
Each method, as we know, has a different return type, such as String, Boolean, Integer, and so on. The Java main() method uses the void return type, which does not return anything. To keep things simple, the main() method returns nothing. After executing the main() method, the program will be terminated, and returning anything from the main() method is useless because the JVM will do nothing with the returned object.
If we return something from the main() method, the following error will occur:
public class CodingNinjasTest {
// do not define main() method as static
public static main(String[] args){
System.out.println("Main method with no return type.");
}
}
Output:
main
It refers to the main() method. The method's name is fixed and cannot be changed. If we attempt to alter the name of the method(), the following error will be thrown:
public class CodingNinjasTest {
// do not define main() method as static
public static void changedMain(String[] args){
System.out.println("changed name of main method.");
}
}
Output:
String[] args or String args[]
In Java, both String[] args and String args[] are used to declare command-line arguments in the main() method. The difference is purely syntactical.
String[] args: This is the preferred style as it follows the standard Java convention, emphasizing that args is an array of strings.
String args[]: This is an older syntax and is allowed for compatibility with C-style array declaration.
Both declarations work the same way, but String[] args is considered cleaner and more consistent with Java's syntax.
Code Example:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, " + args[0]);
}
}
If run with java Main Raj, it will output:
Hello, Raj
Overloading main() Method in Java
In Java, the main() method serves as the entry point for program execution. It has a specific signature:
public static void main(String[] args)
However, Java allows overloading the main() method, which means you can define multiple main() methods with different parameters. These overloaded methods are not used as the entry point but can be invoked like any other method.
Explanation
Original main() Method: The JVM always looks for the public static void main(String[] args) method to start program execution.
Overloaded main() Methods: You can define multiple main() methods with different parameter types, but they will not be called automatically by the JVM. Instead, you can call them explicitly within the standard main() method.
Example
public class MainOverloading {
// Standard main() method
public static void main(String[] args) {
System.out.println("Original main method");
// Calling overloaded main methods
main(10);
main("Hello, Java!");
}
// Overloaded main method with an int parameter
public static void main(int num) {
System.out.println("Overloaded main method with int: " + num);
}
// Overloaded main method with a String parameter
public static void main(String message) {
System.out.println("Overloaded main method with String: " + message);
}
}
You can also try this code with Online Java Compiler
Original main method
Overloaded main method with int: 10
Overloaded main method with String: Hello, Java!
Explanation of the Example
Standard main(): JVM starts execution here and prints "Original main method".
main(int num): This method is called within the standard main() and prints the integer value passed.
main(String message): Another overloaded method prints the string passed to it.
Frequently Asked Questions
What is a main method in Java?
The main() method is the entry point for Java programs. It must be public, static, and void with String[] args as its parameter
Why String args in main method?
String[] args allows command-line arguments to be passed to the program. Each argument is stored as a String in the array for program use.
What are main args?
main args refers to the String[] args parameter in the main() method, which stores command-line arguments passed when running the Java program.
Conclusion
The main method in Java is static because it allows the Java Virtual Machine (JVM) to invoke it without creating an instance of the class. This ensures that the program can start running as soon as the JVM loads the class, providing a clear entry point. By making the main method static, Java simplifies the execution process and ensures that it works consistently across different environments and scenarios.