In this article, we will learn Java main() method in detail. As the name suggest this is the main point of the program, without the main() method the program won’t execute.
What is a main() method in Java?
The main() method is the starting point of the program. JVM starts the execution of program starting from the main() method.
Syntax of main() method:
public static void main(String args[])
public: We have already learned in the access specifier tutorial that public access specifier allows the access of the method outside the program, since we want the JVM to identify the main method and start the execution from it, we want it to be marked “public”. If we use other access modifier like private, default or protected, the JVM wouldn’t recognise the main() method and the program won’t start the execution.
static: The reason the main() method is marked static so that it can be invoked by JVM without the need of creating an object. In order to invoke the normal method, we need to create the object first. However, to invoke the static method we don’t need an object. Learn more about static method here.
void: This is the return type. The void means that the main() method will not return anything.
main(): This the default signature which is predefined by JVM. When we try to execute a program, the JVM first identifies the main() method and starts the execution from it. As stated above, the name of this method suggests that it is the “main” part of the program.
String args[]: The main method can also accepts string inputs that can be provided at the runtime. These string inputs are also known as command line arguments. These strings inputs are stored in the array args[]
of String type.
Can we have main() method defined without String args[] parameter?
If we have a main() method without String args[] in a program, the program will throw no compilation error however we won’t be able to run the program as the JVM looks for the public main method with the String args[] parameter and if it doesn’t find such method, it doesn’t run the program.
Let’s try this:
public class JavaExample { public static void main() { System.out.println("Hello!"); } }
Output:
Error: Main method not found in class JavaExample, please define the main method as: public static void main(String[] args)
As you can see that the program threw error at runtime.
Java – static block vs main method
As we learned in the previous article, static block is used to initialise the static data members. Let’s run a program with static block and main method (static method) to see in which order they run.
class JavaExample { //static block static { System.out.println("Static Block"); } //static method public static void main(String args[]) { System.out.println("Main Method"); } }
Output:
Static Block Main Method
As we can see, the static block executed before the main method.
What if a program doesn’t have a main method?
Let’s write a program without the main method to see whether it runs or not.
class JavaExample { static { System.out.println("Static Block"); } }
Output:
Error: Main method not found in class JavaExample, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
Different ways to write main method in java
The following are the valid ways to write a main method in java:
public static void main(String[] args) //We can interchange static and public static public void main(String[] args) //We can place the square brackets at the different locations public static void main(String[] args) public static void main(String []args)
Overloading of main() method in Java
We can overload the main method in Java. This allows us to have more than one main() method in Java. However the signature of all the overloaded methods must be different. To learn more about overloading, refer this guide: Method overloading in Java.
class JavaExample { public static void main(String args[]) { System.out.println("main method"); main(100); main('A'); } //Overloaded int main method public static void main(int a) { System.out.println(a); } //Overloaded char main method public static void main(char ch) { System.out.println(ch); } }
Output:
main method 100 A
Frequently Asked Questions
The main method is used to specify the starting point of the program. This is the starting point of our program from where the JVM starts execution of the program.
No, you can’t declare a method inside main() method.
Yes we have can more than one main methods in java, however JVM will always calls String[] argument main() method. Other main() methods will act as a Overloaded method. In order to invoke these overloaded methods, we have to call them explicitly.
No, we cannot override main method of java because it is a static method and we cannot override a static method. The static method in java is associated with class which is why we don’t need an object to call these. Therefore, it is not possible to override the main method in java.
Leave a Reply