Let’s break down public static void main(String args[]) in Java step by step. This line is the entry point of any standalone Java application, and each keyword has a specific role:
🔎public static void main(String args[]) – Explanation of Each Part of psvm
public
- Meaning: Access modifier.
- Why: Makes the method accessible from anywhere.
- Reason in main: The JVM (Java Virtual Machine) needs to call this method from outside your class, so it must be public.
static
- Meaning: Belongs to the class, not to any object.
- Why: JVM can call the method without creating an object of the class.
- Reason in main: Since the program starts before any objects exist, main must be static.
void
- Meaning: Return type.
- Why: Indicates the method does not return any value.
- Reason in main: JVM doesn’t expect a return value; it just runs the code inside.
main
- Meaning: Method name.
- Why: Special name recognized by JVM as the starting point of the program.
- Reason in main: Without this exact name, the program won’t run.
String args[]
- Meaning: Parameter (array of String objects).
- Why: Holds command-line arguments passed when running the program.
- Example:
java MyClass Hello World
here, args[0] = “Hello”, args[1] = “World”
- Note: You can also write String[] args (both are valid).
🧩Putting It All Together | psvm Summary
public static void main(String args[]) tells the JVM:
- “Here’s a method (main) you can access (public),
- without creating an object (static),
- that doesn’t return anything (void),
- and can accept command-line input (String args[]).”
✅ Example Program on public static void main:
public class HelloWorld
{
public static void main(String args[])
{
System.out.println(“Hello, World!”);
System.out.println(“First argument: ” + args[0]);
}
}
Run with:
java HelloWorld Gradguru99
Output:
Hello, World!
First argument: Gradguru99
