How to use the Java method main():

The main() method is at the beginning and end of every program in Java. Only statements that are within its scope are taken into account during execution. This is why it is so important to pay attention to every last detail of the syntax of the method. If it’s incorrect, the program won’t be executed.

What is the main() method in Java?

No matter what project you want to implement with the programming language or what concise or extensive code you use for it, In Java, every application starts with the main() method. If it’s not available, the code can’t be executed. Before this is passed to main(), the Java Virtual Machine (JVM) is loaded into memory and the code is compiled. Once these steps have been completed, the code is checked with the Java main() method. It is then hopefully executed correctly. In principle, it is also possible to use several of these methods, whereby the maximum is one main method per class.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups

What is the syntax of the Java method main()?

The syntax of Java’s main() method is always the same. It consists of multiple important parts and looks like this:

public static void main(String[] args)
java

These components fulfill the following functions:

  • public: This component ensures that a class can also be accessed from outside.
  • static: This part declares the method as static, which means that it can exist without an object of a specific class having been created in advance.
  • void: This ensures that the main() method in Java does not have to return a value. -main: This is what Java Virtual Machine uses to identify the method when it executes a program.
  • String[] args: This is an array where the command-line arguments are stored and received.

Why are the individual components of main() important?

To better understand how the Java main() method works and why all the components are so important, we’ll take a look at them individually in the following sections. Most importantly, we’ll show you what happens if part of the signature is missing. To do this we’ll choose a very simple code example, which we’ll output with the Java command System.out.println.

public class main {
	public static void main(String[] args) {
		System.out.println("Here is your sample text.");
	}
}
java

When you execute this code, you’ll receive the following output:

Here is your sample text.
java

public

public acts as an access specifier and signals to Java Virtual Machine where the corresponding program is executed. There are other access modifiers, such as default, private and protected. However, if you use these with the main() method in Java, you’ll receive an error message:

public class main {
	private static void main(String[] args) {
		System.out.println("Here is your sample text.");
	}
}
java

The corresponding issue is:

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

This error message indicates that there is no public access to the method.

static

The keyword static confirms that the method used is static. This means it can be used independently of class instances. If you remove this component, the code will look like this:

public class main {
	public void main(String[] args) {
		System.out.println("Here is your sample text.");
	}
}
java

Here, you also receive an error message:

Error: Main method is not static in class test, please define the main method as:
public static void main(String[] args)
java

void

void is a keyword that specifies that the main() method in Java should not return any values. Since main() is the start and end point of a program in Java, rendering is unnecessary. This is what the code looks like without void:

public class main {
	public static main(String[] args) {
		System.out.println("Here is your sample text.");
	}
}
java

This is the error message that you receive if you leave out void:

Error: Main method must return a value of type void in class Main, please define the main method as:
public static void main(String[] args)
java

main

For every program in Java, the JVM first looks for the identifier main. This indicates where the execution should begin and end. As soon as the main() method in Java completes its execution, the program terminates. This is why the signature should never be changed or added to. Here’s an example of what a change to the signature looks like:

public class main {
	public static void replacementMain(String[] args) {
		System.out.println("Here is your sample text.");
	}
}
java

Since the JVM cannot find the identifier, it will terminate the program directly and issue the following error message:

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

String[] args

The String[] args array corresponds to the java.lang.String class and contains data that it stores as string values. If you omit this specification, the code will look like this:

public class main {
	public static void main() {
		System.out.println("Here is your sample text.");
	}
}
java

Without the argument, the Java main() method cannot be executed. As a result, you’ll receive the following error message again:

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

What alternative spellings are there?

The code example shown here was written according to the most common syntax. However, you can easily adapt it to give yourself a better overview. For example, you can place the square brackets directly after args. Here’s how it would look:

public static void main(String args[]) { }
java

You can also store arguments as variable arguments (varargs). To do this, you need to use an ellipsis:

public static void main(String...args) { }
java
Tip

You can find a range of other articles on Java in our Digital Guide. You can read about how Java and JavaScript compare or learn more about Java operators.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top