Main Method in Java: Syntax, Example
A main method in Java is an entry point to start the execution of a program. Every Java application has at least one class and at least one main method. Normally, an application consists of many classes and only one of the class needs to have a main method.
In other words, a complex program can have dozens of classes, but only one class needs to have a main() method to get things started. Therefore, Java’s main method is the starting place for your program.
Main Method Syntax in Java
The basic syntax to declare a main method in Java program is as follows:
public static void main(String[] args)
{
// Method body goes here.
}
In the above declaration, there are two modifiers, such as public and static, which have been used with the main method. Let’s understand a brief explanation and purpose of each of the terms used in the main method.
1. public: The public modifier makes it accessible from anywhere in the application.
2. static: The static modifier makes it a class method so that it can be called using the class name without creating an object of the class.
3. void: The return type of the main method is void, which means that it does not return a value to its caller. You must specify void when you declare the main method.
4. main: It is the name of a method where execution will start. In Java, the main method is called by JVM.
5. String[ ] args: The main method accepts one argument of type String array (String[ ]). The square brackets [ ] represent the array of strings that are passed as an argument to this method. “args” is the name of its parameter. You can use any parameter name as you wish.
For example, you can declare the main method like this public static void main(String[] myParameter), which is the same as declaring the main method as shown previously.
6. ({): This is an opening brace that marks the beginning of the main method body. We must pair it with a closing brace.
7. Body of main Method: The region between the opening brace and closing brace is called main method body that contains a set of program statements. This region is a static region.
8. (}): This is a closing brace that marks the closing of the main method body. It must be paired with an opening brace.
Why Public Static Void Main in Java?
Basically, the public static void main(String [ ] args) acts as an entry point to start the execution of any Java application program. That’s why we use/write public static void main in a program.
Why do We Declare Main Method as Static in Java?
The main method is declared as static. It is called by JVM when we run a class. The JVM (Java Virtual Machine) does not know how to create an object of a class. It needs a standard way to start the execution of a program. Therefore, the main method is declared as static so that the JVM can call it using the class name which is passed on the command line.
If we do not declare the main method as static, it will be considered as an instance method. The code will be compiled successfully without generating any error message. But at runtime, the code will generate an exception named: “NoSuchmethodError: main”.
How to Call Main Method in Java?
The main method is called by JVM when we run a class.
Can We have More than One main() Method in Class?
Yes, a class can have any number of main() methods but the execution always starts from public static void main(String[ ] args) only. Let’s take an example program where we will declare more than one method.
Example 1:
package javaProgram;
public class MainTest
{
public static void main(int a)
{
System.out.println("Second main() method");
main();
}
public static void main()
{
System.out.println("Third main method");
}
// Main method
public static void main(String[] args)
{
System.out.println("main(String[] args)");
main(20);
}
}
Output: main(String[] args) Second main() method Third main method
In the above example program, we have declared three main() methods. The first main() method is declared as public static void main(String[] args) used as an entry point to run the program.
As far as JVM is concerned, the other two main() methods have no special significance. They have been declared only to print the message on the console.
Can We Execute a Program without Main Method in Java?
Yes, we can execute a program without main() method in Java in the previous version of JDK. One of the ways is a static block. But from onwards JDK 1.7 and above, is not possible. For example:
public class A
{
static
{
System.out.println("Static block is executed");
System.exit(0);
}
}
If the main() method has no argument of array reference of string type, the program source code will compile successfully without generating any error, but at runtime, the program will terminate by generating an exception named: “NoSuchMethodError: main”.
Can We Overload Main Method in Java?
Yes, we can overload the main() method, but we cannot override it. We can declare any number of main() method in a class, but the method signature must be different. Let’s consider a simple example program where we will overload the main method.
Example 2:
package javaProgram;
class OverloadMainMethod
{
public static void main(int a) // Overloaded main method
{
System.out.println(a);
}
public static void main(String args[])
{
System.out.println("main method invoked");
main(10);
}
}
Output: main method invoked 10
Key Points to Remember:
1. public static void main(String args[ ]) is a line at which the program will start executing.
2. The main() method in Java is always called by JVM (Java Virtual Machine) before any objects are created.
3. It does not return any value.
4. Since Java is case-sensitive, Main is different from main. If you type Main instead of main, java compiler would still compile your program but java will report an error because it would not find main() method.
5. If a program does not contain the main method in a class, Java compiler will compile it but cannot run it.
6. The main() method can be overloaded in Java but cannot override it.