Introduction
In Java, public static void main(String[] args) is the traditional entry point of a Java application. When we execute a Java program, the Java launcher looks for a valid main method to start the application.
Let's understand each component.
1. Public
public is an access modifier. It allows the method to be accessed from outside its class. The Java launcher needs access to the main method to start the application.
2. Static
static is a keyword that makes a method belong to the class rather than an individual object.
The main method is static because the Java launcher needs to invoke it without creating an object of the class.
3. Void
void is a return type that indicates the method does not return any value.
For example:
void display() { System.out.println("Hello Java"); } 4. Main
main is the method name recognized by the traditional Java application launcher as the entry point of the program.
5. String[] args
String[] args is an array parameter used to receive command-line arguments. These arguments are passed when executing the program through the terminal.
Example:
class Student { public static void main(String[] args) { System.out.println(args[0]); } } Execute:
java Student Abishek Output:
Abishek Here, args[0] accesses the first command-line argument.
How Does Java Execute the Main Method?
- We write Java source code.
- The
javaccompiler converts it into bytecode. - The Java launcher starts the application.
- The JVM loads and initializes the required class.
- The launcher invokes the main method.
- The statements inside main are executed.
Conclusion
Understanding public static void main(String[] args) is essential for every Java programmer. It explains how a Java application starts, how the launcher invokes the main method, and how command-line arguments are passed to a program.
