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)
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)
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");
}
}
Execution starts from:
public static void main(String[] args)
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)
What Does public Mean?
public
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)
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
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);
But when the JVM starts the program, no object exists yet.
So Java allows the JVM to call:
Main.main(args);
directly.
That is why the method must be static.
What Does void Mean?
void
means the method does not return any value.
Example:
public static int add(int a, int b)
returns an integer.
But:
public static void main(String[] args)
does not return anything.
Its job is simply to start and run the program.
What Does main Mean?
main
is the method name.
The JVM specifically looks for a method named:
main
If you rename it:
public static void start(String[] args)
the program will compile but will not run.
Example:
public class Main {
public static void start(String[] args) {
System.out.println("Hello");
}
}
Running:
java Main
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
means:
An array of String values.
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]);
}
}
Compile:
javac Main.java
Run:
java Main Ebenezer
Output:
Payilagam
Where did "Payilagam" come from?
The JVM automatically creates:
String[] args = {
"Payilagam"
};
and passes it into the main method.
Multiple Command-Line Arguments
Run:
java Main Payilagam Chennai Java
The JVM creates:
String[] args = {
"Payilagam",
"Chennai",
"Java"
};
Memory view:
args[0] -> Payilagam
args[1] -> Chennai
args[2] -> Java
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]);
}
}
Output:
Ebenezer
Chennai
Java
Why Does Java Use an Array?
Because users can pass any number of arguments.
Example:
java Main
No arguments.
Or:
java Main A B C D E F G
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)
This is also valid:
public static void main(String[] students)
And this:
public static void main(String[] names)
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]);
}
}
Run:
java Main Java
Output:
Java
Why Do Developers Still Use args?
Convention.
Just like developers usually write:
for(int i = 0; i < 10; i++)
instead of:
for(int counterVariable = 0; counterVariable < 10; counterVariable++)
they use:
String[] args
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);
}
}
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);
}
}
Run:
java Calculator 10 20
Output:
30
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
The JVM roughly behaves like this:
String[] args = {
"Apple",
"Mango",
"Orange"
};
Main.main(args);
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
or
double[] args
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]);
Common Beginner Mistakes
Accessing Non-Existing Arguments
System.out.println(args[0]);
If no argument is provided:
java Main
you will get:
ArrayIndexOutOfBoundsException
Always check:
if(args.length > 0)
before accessing values.
Renaming the Main Method
This will compile:
public static void start(String[] args)
but it will not run.
The JVM only searches for:
main
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
Final Thoughts
The line:
public static void main(String[] args)
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.
-
publicallows the JVM to access it. -
staticallows it to be called without creating an object. -
voidmeans it returns nothing. -
String[] argsstores command-line arguments. -
argsis 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
Oracle Java Tutorials – Command-Line Arguments
https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.htmlOracle Java Tutorials – A Closer Look at the Hello World Application
https://docs.oracle.com/javase/tutorial/getStarted/application/index.htmlOracle Java Command Documentation
https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.htmlOracle Java Language Specification – JVM Startup
https://docs.oracle.com/en/java/javase/26/docs/specs/jls/jls-12.htmlGeeksforGeeks – Command Line Arguments in Java
https://www.geeksforgeeks.org/java/command-line-arguments-in-java/DigitalOcean – Command Line Arguments in Java
https://www.digitalocean.com/community/tutorials/command-line-arguments-in-java