public static void main(String args[]) – explanation of each term

  • Post author:

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

  • 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).

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[]).”

public class HelloWorld

{

    public static void main(String args[])

{

        System.out.println(“Hello, World!”);

        System.out.println(“First argument: ” + args[0]);

    }

}

java HelloWorld Gradguru99

Hello, World!

First argument: Gradguru99

GradGuru

The founder of GradGuru99 is an alumnus of NIT Durgapur. Through this platform, he and his team provide valuable resources and insightful content tailored for undergraduate students and recent graduates.