DEV Community

Cover image for Java Main Method Explained: Understanding `public static void main(String[] args)` from Scratch
Ebenezer
Ebenezer

Posted on

Java Main Method Explained: Understanding `public static void main(String[] args)` from Scratch

Good Day...

During yesterday's training session, my trainer asked a simple yet interesting question:

"What is args in Java? Can we rename it? How are arguments passed to a Java program, and how does the program print them?"

To be honest, I didn't have a clear answer at that moment. Instead of guessing, I treated it as a learning opportunity and took it as homework.

After the session, I explored Oracle's official Java documentation, read multiple technical articles, and went through community discussions to understand the concept thoroughly. What initially seemed like a small topic turned out to be an important part of understanding how Java applications start and receive input.

In this blog, I'll break down everything I learned about the main() method and String[] args in a beginner-friendly way.

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

When I started learning Java, I knew that every program needed a main() method, but I didn't know:

  • Why is it called main?
  • Why is it public?
  • Why is it static?
  • Why does it return void?
  • What exactly is String[] args?
  • Why does every Java program start here?

If you have the same questions, this article is for you.

By the end of this blog, you'll understand what the Java main method is, why it exists, how args works, and when command-line arguments are actually used in real-world applications.


What Is the Main Method?

The main() method is the entry point of a Java application.

Think of it as the front door of your program.

When the Java Virtual Machine (JVM) starts your application, it looks for a specific method:

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

If the JVM cannot find this method, the program will not start.

According to Oracle's Java documentation, the JVM starts execution by invoking a main method of a specified class. If the method has a parameter, the JVM passes a single argument that is an array of strings.


A Simple Java Program

public class Main {

    public static void main(String[] args) {

        System.out.println("Hello World");

    }

}
Enter fullscreen mode Exit fullscreen mode

Execution starts from:

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

and then moves line by line.


Breaking Down the Main Method

Let's split the signature into smaller pieces.

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

What Does public Mean?

public
Enter fullscreen mode Exit fullscreen mode

means the method can be accessed from anywhere.

The JVM needs to call this method.

If the method were private:

private static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

the JVM would not be able to access it.

That's why Java requires the main method to be public.

Think of it like this:

  • Public = Everyone can enter
  • Private = Only the owner can enter

Since the JVM is outside your class, it needs public access.


What Does static Mean?

static
Enter fullscreen mode Exit fullscreen mode

means the method belongs to the class itself rather than an object.

Without static, Java would require an object before calling the method.

Example:

Main obj = new Main();
obj.main(args);
Enter fullscreen mode Exit fullscreen mode

But when the JVM starts the program, no object exists yet.

So Java allows the JVM to call:

Main.main(args);
Enter fullscreen mode Exit fullscreen mode

directly.

That is why the method must be static.


What Does void Mean?

void
Enter fullscreen mode Exit fullscreen mode

means the method does not return any value.

Example:

public static int add(int a, int b)
Enter fullscreen mode Exit fullscreen mode

returns an integer.

But:

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

does not return anything.

Its job is simply to start and run the program.


What Does main Mean?

main
Enter fullscreen mode Exit fullscreen mode

is the method name.

The JVM specifically looks for a method named:

main
Enter fullscreen mode Exit fullscreen mode

If you rename it:

public static void start(String[] args)
Enter fullscreen mode Exit fullscreen mode

the program will compile but will not run.

Example:

public class Main {

    public static void start(String[] args) {

        System.out.println("Hello");

    }

}
Enter fullscreen mode Exit fullscreen mode

Running:

java Main
Enter fullscreen mode Exit fullscreen mode

produces an error because the JVM cannot find the entry point.


What Is String[] args?

This is the part that confuses most beginners.

String[] args
Enter fullscreen mode Exit fullscreen mode

means:

An array of String values.
Enter fullscreen mode Exit fullscreen mode

The JVM uses this array to pass information to your application when it starts.

Let's understand this with an example.


What Are Command-Line Arguments?

Suppose we have this program:

public class Main {

    public static void main(String[] args) {

        System.out.println(args[0]);

    }

}
Enter fullscreen mode Exit fullscreen mode

Compile:

javac Main.java
Enter fullscreen mode Exit fullscreen mode

Run:

java Main Ebenezer
Enter fullscreen mode Exit fullscreen mode

Output:

Payilagam
Enter fullscreen mode Exit fullscreen mode

Where did "Payilagam" come from?

The JVM automatically creates:

String[] args = {
    "Payilagam"
};
Enter fullscreen mode Exit fullscreen mode

and passes it into the main method.


Multiple Command-Line Arguments

Run:

java Main Payilagam Chennai Java
Enter fullscreen mode Exit fullscreen mode

The JVM creates:

String[] args = {
    "Payilagam",
    "Chennai",
    "Java"
};
Enter fullscreen mode Exit fullscreen mode

Memory view:

args[0] -> Payilagam
args[1] -> Chennai
args[2] -> Java
Enter fullscreen mode Exit fullscreen mode

Example:

public class Main {

    public static void main(String[] args) {

        System.out.println(args[0]);
        System.out.println(args[1]);
        System.out.println(args[2]);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output:

Ebenezer
Chennai
Java
Enter fullscreen mode Exit fullscreen mode

Why Does Java Use an Array?

Because users can pass any number of arguments.

Example:

java Main
Enter fullscreen mode Exit fullscreen mode

No arguments.

Or:

java Main A B C D E F G
Enter fullscreen mode Exit fullscreen mode

Seven arguments.

Since the number of inputs is not fixed, Java stores them inside an array.

Oracle's documentation explains that command-line arguments are passed to the application's main method through an array of strings.


Can We Rename args?

Yes.

This surprises many beginners.

This is valid:

public static void main(String[] myInput)
Enter fullscreen mode Exit fullscreen mode

This is also valid:

public static void main(String[] students)
Enter fullscreen mode Exit fullscreen mode

And this:

public static void main(String[] names)
Enter fullscreen mode Exit fullscreen mode

The JVM does not care about the parameter name.

The JVM only cares that:

  • Method name is main
  • Method is public
  • Method is static
  • Parameter type is a String array

Example:

public class Main {

    public static void main(String[] students) {

        System.out.println(students[0]);

    }

}
Enter fullscreen mode Exit fullscreen mode

Run:

java Main Java
Enter fullscreen mode Exit fullscreen mode

Output:

Java
Enter fullscreen mode Exit fullscreen mode

Why Do Developers Still Use args?

Convention.

Just like developers usually write:

for(int i = 0; i < 10; i++)
Enter fullscreen mode Exit fullscreen mode

instead of:

for(int counterVariable = 0; counterVariable < 10; counterVariable++)
Enter fullscreen mode Exit fullscreen mode

they use:

String[] args
Enter fullscreen mode Exit fullscreen mode

because everyone immediately recognizes it.


Can We Use args in Our Own Methods?

Absolutely.

Example:

public static void printNames(String[] args) {

    for(String name : args) {

        System.out.println(name);

    }

}
Enter fullscreen mode Exit fullscreen mode

Here, args is just a parameter name.

It has nothing to do with the JVM.

You can rename it to anything.


Real-World Example: Calculator

Suppose we want to add two numbers.

public class Calculator {

    public static void main(String[] args) {

        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);

        System.out.println(a + b);

    }

}
Enter fullscreen mode Exit fullscreen mode

Run:

java Calculator 10 20
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

The values are provided at runtime.

No code changes required.


Where Are Command-Line Arguments Used in Real Projects?

Many beginners think command-line arguments are only academic examples.

In reality, they are used extensively.

Examples include:

  • Batch processing jobs
  • Build tools
  • Database migration tools
  • Java compiler commands
  • Server startup configurations
  • Automation scripts
  • Cloud deployments

Oracle's Java command documentation states that entries following the class name become arguments for the main method.


What Happens Internally?

Suppose you run:

java Main Apple Mango Orange
Enter fullscreen mode Exit fullscreen mode

The JVM roughly behaves like this:

String[] args = {
    "Apple",
    "Mango",
    "Orange"
};

Main.main(args);
Enter fullscreen mode Exit fullscreen mode

This is not the exact JVM source code, but conceptually this is what happens.

The JVM creates the array and passes it into the main method.


Why Are Command-Line Arguments Strings?

You might wonder:

Why not:

int[] args
Enter fullscreen mode Exit fullscreen mode

or

double[] args
Enter fullscreen mode Exit fullscreen mode

The JVM receives user input as text.

Everything typed in a terminal starts as characters.

Therefore Java safely stores all inputs as strings first and lets developers convert them later when needed.

Example:

int age = Integer.parseInt(args[0]);
Enter fullscreen mode Exit fullscreen mode

Common Beginner Mistakes

Accessing Non-Existing Arguments

System.out.println(args[0]);
Enter fullscreen mode Exit fullscreen mode

If no argument is provided:

java Main
Enter fullscreen mode Exit fullscreen mode

you will get:

ArrayIndexOutOfBoundsException
Enter fullscreen mode Exit fullscreen mode

Always check:

if(args.length > 0)
Enter fullscreen mode Exit fullscreen mode

before accessing values.


Renaming the Main Method

This will compile:

public static void start(String[] args)
Enter fullscreen mode Exit fullscreen mode

but it will not run.

The JVM only searches for:

main
Enter fullscreen mode Exit fullscreen mode

as the entry point.


Assuming args Is a Keyword

args is not a Java keyword.

It is simply a variable name.

These are all valid:

String[] args
String[] names
String[] students
String[] input
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

The line:

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

may look intimidating when you're learning Java for the first time, but it becomes much easier once you break it into pieces.

Remember these key points:

  • main() is the entry point of a Java application.
  • The JVM starts execution from the main method.
  • public allows the JVM to access it.
  • static allows it to be called without creating an object.
  • void means it returns nothing.
  • String[] args stores command-line arguments.
  • args is only a convention and can be renamed.
  • Command-line arguments allow users to provide input when the application starts.

Instead of memorizing the main method signature, try to understand the responsibility of each part. Once you do, the line that once looked mysterious becomes one of the simplest concepts in Java.

References

  1. Oracle Java Tutorials – Command-Line Arguments
    https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

  2. Oracle Java Tutorials – A Closer Look at the Hello World Application
    https://docs.oracle.com/javase/tutorial/getStarted/application/index.html

  3. Oracle Java Command Documentation
    https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html

  4. Oracle Java Language Specification – JVM Startup
    https://docs.oracle.com/en/java/javase/26/docs/specs/jls/jls-12.html

  5. GeeksforGeeks – Command Line Arguments in Java
    https://www.geeksforgeeks.org/java/command-line-arguments-in-java/

  6. DigitalOcean – Command Line Arguments in Java
    https://www.digitalocean.com/community/tutorials/command-line-arguments-in-java

Top comments (0)