Scientech Easy Java Main Method | Example Program | Scientech Easy

Saturday, April 13, 2019

Java Main Method | Example Program

In the previous tutorial, you learned about declaring a method in a class. Let's discuss the main() method that you have been using to execute your class. 

Main() method in Java

A main() method 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. The declaration of the main method is as follows:
Syntax of Java main method:
    public static void main(String[] args){
         // Method body goes here.
Main method
In the above declaration, two modifiers such as public, and static has been used with the main method. Let's see a brief explanation about it.
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[ ]). 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.

Why do we need to declare the main method as static in Java?

The main method is declared as static. Basically, the main() method acts as an entry point for a java application. It is called by JVM when we run a class. The JVM 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.

Why the main method declared as static in Java?

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". 

Can we have more than one main() method in a 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 see an example program.
Program source code 1:
    package basicexample; 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"); } 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 for 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, it is not possible.
For example:
    public class A{ static { System.out.println("Static block is executed"); System.exit(0); } }
Key points: 
1. If the main() method has no argument of array reference of string type, the program source code will be compiled successfully without generating any error but at runtime, the program will terminate by generating an exception named: "NoSuchMethodError: main".
2. The main() method can be overloaded but not override.
Final words: 
Hope that this tutorial has covered almost all the important points related to Java main() method. I hope that you will have enjoyed this tutorial.
Thanks for reading!
Next ➤ How to call a method with parameter in Java 

⏪ Prev Next ⏩