Main Method in Java | public static void main(String[] args)

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 syntax for declaration of the java main method is as follows:
Syntax:
        public static void main(String[] args)
        {
            // Method body goes here.
        }

main method in java


In the above declaration, two modifiers such as public, and static has been used with the main method. Let’s see 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 is 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. It must be paired with a closing brace.

7. main Method Body: The region between the opening brace and closing brace is called main method body that contains a set of program statements. This region is 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 Java application program. That’s why we use/write public static void main in java program.

Why do we need to 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 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 main method is 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”.

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.
Program source code 1:

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 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, it is not possible.
For example:

public class A
{ 
  static 
  { 
     System.out.println("Static block is executed"); 
     System.exit(0); 
   } 
}

Key points: 
👉 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”.

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 make a program where we will overload the main method.
Program source code 2:

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

Final words: 

Hope that this tutorial has covered almost all the important points related to Java main() method. I hope that you will have understood and enjoyed this tutorial.
Thanks for reading!!!
Next ⇒ Arguments in Java⇐ PrevNext ⇒ 

 

Leave a Comment