Java Main Method - Public Static Void Main (String[] args)

In Java programs, the Java main method is the first thing that runs when you start a program, which is why it's usually one of the first things you learn. It acts as the starting point of the program and can either contain the main logic or call other methods to do the work. You can put the main method inside any class, but in larger projects, there's often a separate class just for it. This class can have any name, but it's often named Main.

This article will explain the meaning of each part of the Java main method.

Java main() Method Syntax (Java main Method)

The basic syntax of the Java main method is given below:

copy

public static void main(String[] args) {

// …

}

The only part in the main Java you’re allowed to change is the name of the String array parameter. For instance, instead of args, you could use a different name like myStringArgs. You can also write the parameter in different forms, such as String... args or String args[]—they all mean the same thing.

  1. public

The public keyword is an access modifier that determines who can access the method. Declaring the main() method as public means it can be accessed from anywhere, including by the Java Virtual Machine (JVM), even though the JVM is outside the class.

If you don’t make the main() method public, the JVM won’t be able to run your program, and you’ll get an error.

Example:

copy

// Java program using private instead of public for main() method

public class Main {

private static void main(String[] args) {

System.out.println("Hello from Main");

}

}

In this example, because the main method is private, the JVM cannot access it to start the program, which will result in a runtime error.

  1. static

The static keyword means the method belongs to the class rather than an instance of it. The main() method is static, so the JVM can run it without creating an object of the class. This saves memory and speeds up execution.

If the main() method isn’t static, you’ll see an error when you try to run the program.

Example:

copy

// Java program without static main method

class ExampleTwo {

public void main(String[] args) {

System.out.println("Hello from ExampleTwo");

}

}

Error: Main method not found or not accessible.

  1. void

The void keyword means that the method does not return any value. Since the main() method is the starting point and not meant to return anything to the JVM, it must be declared as void.

If you try to give it a return type like int, the program won’t run correctly.

Example:

copy

// Java program with wrong return type

class ExampleThree {

public static int main(String[] args) {

System.out.println("Hello from ExampleThree");

return 0;

}

}

Output:

Error: Main method must return void.

  1. main

The word main is the special name that JVM looks for to start program execution. It’s not a keyword—you name it main because the JVM expects that exact name.

If you rename the method to anything else, the program will not run.

Example:

copy

// Java program with incorrect method name

class ExampleFour {

public static void startHere(String[] args) {

System.out.println("Hello from ExampleFour");

}

}

Output:

Error: Main method not found.

  1. String[] args

This is the parameter for the main() method. It's used to accept command-line arguments passed when you run the Java program. You can change the name of the variable (e.g., args to input), but the type and structure must remain the same.

Example:

copy

// Java program to print command-line arguments

class ExampleFive {

public static void main(String[] input) {

for (String item : input) {

System.out.println(item);

}

}

}

Output (if run with arguments “Java Rules”):

copy

Java

Rules

You can also write the main method using different syntax styles like:

  • public static void main(String args[])

  • public static void main(String... args)

As long as it accepts a String array or varargs of String, the JVM will treat it as the main entry point.

The most commonly used way to write the main() method in Java is shown in the following example:

Example

copy

// Java Program showing the basic

// structure of the main() method

class HelloWorld {

public static void main(String[] input) {

System.out.println("Hello, Java!");

}

}

In this example, the class is named HelloWorld, and it contains the Java main method, which prints a simple message to the screen. The name of the String array parameter has been changed from args to input, which is allowed in Java.

Overloading the Java main Method

In Java, it's possible to overload the Java main method. This means you can define multiple main() methods in the same class as long as each one has a different parameter list (method signature). While the JVM will only call the version with String[] args to start the program, the other versions can be called manually from within your code.

Example: Overloading main() with Different Parameters

copy

public class DemoMain {

public static void main(String[] args) {

if (args.length == 0) {

System.out.println("Standard main() with no input");

} else if (args.length == 1) {

if (args[0].equalsIgnoreCase("hello")) {

main("Welcome to Java!");

} else {

try {

double number = Double.parseDouble(args[0]);

main(number);

} catch (NumberFormatException e) {

System.out.println("Invalid input format.");

}

}

}

}

// Overloaded Java main method that accepts a String

public static void main(String message) {

System.out.println("Message received: " + message);

}

// Overloaded main method that accepts a double

public static void main(double value) {

System.out.println("Numeric input received: " + value);

}

}

Output (when run without arguments):

Conclusion

In this guide, you explored the different parts that make up the Java main method and understood their purpose. Keep expanding your Java knowledge by exploring more tutorials and hands-on examples.

Get powerful, secure, and high-performance VPS hosting with Host-World. Choose from top VPS plans built for speed, reliability, and a smooth online experience.

Supercharge your website with Host-World VPS today!

About the author
Oleksandr Vlasenko
Oleksandr Vlasenko

Oleksandr Vlasenko, Head of Growth at Host-World, is an experienced SEO and growth strategist with over 10 years of expertise in driving organic traffic and scaling businesses in hosting, e-commerce, and technology. He holds a master's degree... See All

Leave your reviews

Share your thoughts and help us improve! Your feedback matters to us

Upload your photo for review