Main method
Introduction
The main method, also known as the entry point, is the starting point of any Java application.
When a Java program is executed, the main method is called first by the Java Virtual Machine (JVM).
public static void main(String[] args) {
// Program code goes here
}
Breaking down the syntax
Access Modifier: The public access modifier ensures that the main method is accessible from anywhere within the program.
Static Modifier: The static keyword indicates that the main method belongs to the class itself, not to any specific instance of the class.
Return Type: The void return type signifies that the main method doesn't return a value.
Method Name: The name of the method is main, which is the standard identifier for the entry point of a Java program.
Parameter List: The parameter list consists of an array of strings named args. This array holds any command-line arguments passed to the program when it's executed.
In simpler terms
This line of code is like a sign that says "Start Here!"
It directs the Java program on where to begin executing your code.
The public and static keywords ensure that this starting point is easily accessible, and the void main part clarifies that it doesn't provide any output at the end.
The String[] args is like an extra compartment you can use to provide text information to the program if needed.
Remember, the actual instructions (code) that the program will execute come after this line, inside the curly braces { }.
Execution of the Main Method
- When you run a Java program, the JVM first identifies the class containing the main method.
- It then creates an instance of that class and invokes the main method, passing any command-line arguments as an array of strings.
- The main method executes the program code, and when it finishes, the program terminates.