The main() method is the entry point of any Java application. When you run a Java program, the JVM starts execution from this method. Without it, a Java program cannot run independently.
public and staticvoidThe standard syntax of the Java main() method is fixed so that the JVM can locate and execute it correctly. Each keyword has a specific purpose.
public → Accessible by JVM from anywherestatic → Can be called without creating an objectvoid → Does not return any valueString[] args → Stores command-line arguments
// Basic structure of main method in Java
public class MainExample {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
When the program runs, the JVM finds the main() method and executes the statement inside it. The text Hello, Java! is printed on the console.
// main method with command-line arguments
public class ArgsExample {
public static void main(String[] args) {
System.out.println("First argument: " + args[0]);
}
}
public is removed