Why we need the main method in Java?

Whether the class contains the main() method or not, and whether it is properly declared or not, these checkings are not responsibilities of the compiler, at runtime JVM is responsible for this. If JVM is unable to find the required main() method then we will get runtime exception saying NoSuchMethodError: main.

Java main method


 
class Test 
{}
Output:
javac Test.java
java Test R.E: NoSuchMethodError: main
  

main method breakdown

 
public static void main(String[] args) {

    }
  

  • public to call by JVM from anywhere.
  • static without object creation JVM has to call this method.
  • void main method won't return anything to JVM.
  • main(String args[]) accept command-line arguments.

the modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv"

Even though the above syntax is very strict but the following changes are acceptable to the main() method. The order of modifiers is not important that is instead of public static we can take static public.

We can declare string[] in any acceptable form.
 
String[] args
String []args
String args[]
   

Instead of args, we can use any valid java identifier. We can replace string[] with the var-arg parameter.

Example: main(String... args) the main() method can be declared with the following modifiers. final, synchronized, strictfp.

  
class Test { 
 static final syncronized strictfp public void  main(String... ask){
  System.out.println("valid  main method");
 }
}
output :
valid main method
  

Which of the following main() method declarations are valid ? 1). public static void main(String args){} (invalid) 2). public synchronized final strictfp void main(String[] args){} (invalid) 3). public static void Main(String... args){} (invalid) 4). public static int main(String[] args){} //int return type we can't take //(invalid) 5). public static synchronized final strictfp void main(String... args){}(valid) 6). public static void main(String... args){}(valid) 7). public void main(String[] args){}(invalid)

Passing Arguments to the main() Method

You can pass arguments from the command line to the main() method. This command-line shows below:

 
java -cp classes org.javacodestuffs.Test Hello World!!!    

When the JVM executes the main() method of the org.javacodestuffd.Test, the String array passed as a parameter to the main() method will contain two Strings: "Hello" and "World!!".

 
package org.javacodestuffs;

public class Test {

    public static void main(String[] args) {
        System.out.println( args[0] );
        System.out.println( args[1] );
    }
}

output: 
Hello
World!!!
   

Jdk 7 changes for the main method

Since Jdk 7 the main method is compulsory. 

The compiler will verify first, whether the main() is present or not. If your program doesn't contain the main method, then you will get an error "main method not found in the class".

 
// This program will successfully run 
// prior to JDK 7 
public class Test  
{ 
    // static block 
    static
    { 
        System.out.println("Hello User"); 
    } 
} 

  

Can we write a method inside the main method in java?

Can the main method be private in java?

Can we inherit the main method in java?

Why the main method does not return any value in java?

Why the main method is public in java?

The main method is public in java so the main method 's variables and constants can be accessed by all functions inside that class.



In this article, we have seen Why we need main method in Java detailed explaination of main method.