Report this

What is the reason for this report?

public static void main(String[] args) - Java main method

Updated on November 5, 2022
public static void main(String[] args) - Java main method

Introduction

The Java main method is usually the first method you learn about when you start programming in Java because its the entry point for executing a Java program. The main method can contain code to execute or call other methods, and it can be placed in any class that’s part of a program. More complex programs usually have a class that contains only the main method. The class that contains the main method can have any name, although typically you can just call the class Main.

In the examples that follow, the class that contains the main method is called Test:

Test.java
public class Test {

	public static void main(String[] args){

		System.out.println("Hello, World!");
	
	}
}

In this article you’ll learn what each component of the main method means.

Java Main Method Syntax

The syntax of the main method is always:

public static void main(String[] args){
	// some code
}

You can change only the name of the String array argument. For example, you can change args to myStringArgs. The String array argument can be written as String... args or String args[].

public

The access modifier of the main method needs to be public so that the JRE can access and execute this method. If a method isn’t public, then access is restricted. In the following example code, the main method is missing the public access modifier:

Test.java
public class Test {

	static void main(String[] args){

		System.out.println("Hello, World!");
	
	}
}

When you compile and run the program, the following error occurs because the main method isn’t public and the JRE can’t find it:

  1. javac Test.java
  2. java Test
Output
Error: Main method not found in class Test, please define the `main` method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

static

When the Java program starts, there is no object of the class present. The main method has to be static so that the JVM can load the class into memory and call the main method without creating an instance of the class first. In the following example code, the main method is missing the static modifier:

Test.java
public class Test {

	public void main(String[] args){

		System.out.println("Hello, World!");
	
	}
}

When you compile and run the program, the following error occurs because the main method isn’t static:

  1. javac Test.java
  2. java Test
Output
Error: Main method is not static in class Test, please define the `main` method as: public static void main(String[] args)

void

Every Java method must provide the return type. The Java main method return type is void because it doesn’t return anything. When the main method is finished executing, the Java program terminates, so there is no need for a returned object. In the following example code, the main method attempts to return something when the return type is void:

Test.java
public class Test {

	public static void main(String[] args){
	
		return 0;
	}
}

When you compile the program, the following error occurs because Java doesn’t expect a return value when the return type is void:

  1. javac Test.java
Output
Test.java:5: error: incompatible types: unexpected return value return 0; ^ 1 error

main

The Java main method is always named main. When a Java program starts, it always looks for the main method. The following example code shows a main method renamed to myMain:

Test.java
public class Test {

	public static void myMain(String[] args){

		System.out.println("Hello, World!");
	}
}

When you compile and run the program, the following error occurs because the JRE can’t find the main method in the class:

  1. javac Test.java
  2. java Test
Output
Error: Main method not found in class Test, please define the `main` method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

String[] args

Java main method accepts a single argument of type String array. Each string in the array is a command line argument. You can use command line arguments to affect the operation of the program, or to pass information to the program, at runtime. The following example code shows how to print the command line arguments that you enter when you run the program:

Test.java
public class Test {

	public static void main(String[] args){

    	for(String s : args){
		System.out.println(s);
    	}
	
    }
}

When you compile the program and then run it with a few command line arguments separated by spaces, the arguments get printed in the terminal:

  1. javac Test.java
  2. java Test 1 2 3 "Testing the main method"
Output
1 2 3 Testing the main method

Conclusion

In this article you learned about each component of the Java main method. Continue your learning with more Java tutorials.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Andrea Anderson
Andrea Anderson
Editor
Technical Editor
See author profile
Category:
Tags:

Still looking for an answer?

Was this helpful?

Nice explanation on core main method.

- Sambasiva

Excellent! Given more clarity on main method

- Vinodkumar

Sir I want to implement push notifications like your site. My website is built on spring and jsp. Please help me out of this.

- Abhishek Tripathi

not working

- Priyanka

Nice explanation on core main method.

- Naveen singh

Can you explain as to why do we need to have a String args[] array passed to main? Why does it not compile if we do not pass a String args[] array? Why does Java need a String args[] array?

- Duke

Sir I still haven’t understood the significance of String [] args. Please explain.

- Kanchan Gautam

I think there is a small mistake in saying "Java programming mandates that every method signature provide the return type. ". Return type is not part of the method signature. Nice explanation though!

- Cacarian

Very Nice explanation about java main method…each part…Great

- Vijay

awsome explanation sir,keep going ,

- p.naveen

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.