java main methodjava main method
HappyCoders Glasses

Java main()
Method With 2024/2025 Enhancements

Sven Woltmann
Sven Woltmann
Last update: May 6, 2025

In this article, you will learn all about the main method in Java – the starting point of every Java program. The article also describes the enhancements in Java 21 to 24, which are still in the preview stage.

You will learn in detail:

  • What is a main() method, and what do we need it for?
  • How do you run the main() method?
  • What are the components of Java’s main() method, and what do they mean?
  • How can the main() method be written much more easily in new Java versions?

What Is the main() Method in Java?

To start a Java program, it requires a main method. This method is the entry point to the program. The JVM (Java Virtual Machine) calls this main method when a program is started and executes the Java code it contains.

Example of a main() method in Java

A simple Hello World Java program with a main method looks like this, for example:

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}Code language: Java (java)

To start this program, first, save the program code in a file named HelloWorld.java.

Then enter the following command on the command line / in a terminal:

java HelloWorld.javaCode language: plaintext (plaintext)

You should now see the following output:

Hello world!Code language: plaintext (plaintext)

Congratulations! You have written and started your first Java program.

But why was it so complicated?

What do all the terms like public, class, static, void, etc. mean in the program code?

The good news is that, as a beginner, you don’t need to know this at first. In modern Java versions, it is much easier!

How? You will see this in the following section.

(If you are still interested, you will find a detailed description of all components of the main method below in the section Java main() method syntax).

Simplified main() Method

The main method has been greatly simplified in Java 21. However, this change is still in the preview stage until at least Java 24. This means that details may still change and that you should not use the new main method in production code yet. The new main method is expected to be finalized in Java 25.

In this section, I will show you what will change for you. You can find the technical details behind these changes in the Compact Source Files and Instance Main Methods section.

Let’s go back to the Hello World example from the first section:

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}Code language: Java (java)

You had to write a lot of boilerplate code here – code that would be the same in every Java program. The only thing you want to say is: “Please print the text ‘Hello World!’.”

In Java 21 and the current Java version 24 (with activated preview features), you can already write this much shorter:

void main() {
  IO.println("Hello world!");
}Code language: Java (java)

The main method and the output command println are still there – but a lot of boilerplate code around them have been deleted, and System.out.println() has been shortened to IO.println().

This means that as a Java beginner, you will no longer have to worry about public, class, static, etc., in the future. These terms can be introduced when they are needed.

Please note that you must explicitly activate preview features. If you save the Java 24 variant in a file HelloWorld24.java, then you must start the program as follows:

java --enable-preview --source 24 HelloWorld24.javaCode language: plaintext (plaintext)

The following section explains the technical details behind the changes.

Compact Source Files and Instance Main Methods

(In Java 24, this feature was called: “Simple Source Files and Instance Main Methods”.
Before Java 24, this feature was called: “Implicitly Declared Classes and Instance Main Methods”.)

In the previous section, you learned that in the future, a Java main method can be written without a class, without public static, and without String[] args, – and that instead of using System.out.println(…) for output, the shorter IO.println(…) is sufficient.

This makes the following code a valid and complete Java program in the future:

void main() {
  IO.println("Hello world!");
}Code language: Java (java)

This section describes in detail the changes that have made this simplification possible.

First of all, the history of the changes:

In the following, I describe the four components of the new feature: compact source files, implicitly declared classes, instance-main methods, and the automatically imported java.io.IO class.

Compact Source Files

A Java file without an explicit class declaration, i.e., without a public class HelloWorld, for example, is referred to as a “compact source file”.

Implicitly Declared Classes

From the code contained in a compact source file, the Java compiler generates a so-called “implicitly declared class” with a name defined by the compiler. As a rule, this is the file’s name without the .java extension.

For example, if you compile the file HelloWorld.java, the compiler creates the file HelloWorld.class – and if you then decompile it, you will see that the class name is also HelloWorld.

The following characteristics apply to implicitly declared classes:

  • An implicitly declared class is always in the unnamed package (just like any regular class without package definition).
  • An implicitly declared class is generally final, so it cannot be inherited.
  • An implicitly declared class cannot implement interfaces or extend other classes.
  • An implicitly declared class cannot be accessed via the name specified by the compiler, i.e., other classes cannot instantiate an implicitly declared class, and they cannot call any methods on it, not even static ones.

However, an implicitly declared class can call methods on itself, i.e., methods that are defined in the same .java file, as in the following example:

void main() {
  println(greeting());
}

String greeting() {
  return "Hello, World!";
}Code language: Java (java)

Since an implicitly declared class cannot be accessed from outside, it must always contain a main method.

Instance Main Methods

Instance main methods are non-static main methods, i.e., main methods without the static keyword. The following main methods will be permitted in the future:

  • non-static instance methods,
  • Methods with the visibility level public, protected, or package-private (without a modifier),
  • Methods with or without String[] parameters.

Here are a few examples:

  • void main()
  • void main(String[] args)
  • public void main()
  • protected static void main(String[] args)

Static and non-static methods with the same signature, as well as methods with different visibility modifiers with the same signature, are mutually exclusive and lead to a “method is already defined” compiler error.

However, it is possible for a main method with a String[] parameter and a main method without parameters to exist simultaneously in the same .java file:

void main(String[] args) {
  . . .
}

protected static void main() {
  . . .
}Code language: Java (java)

In this case, the JDK developers specified that the method with the String[] parameter has priority. In the example, the JVM would start void main(String[] args).

Console Interaction With java.io.IO / java.lang.IO

In the third preview phase of the changes, introduced in Java 23, the new class java.io.IO was added – with the following static methods:

  • void print(Object obj) – prints the passed text or the text representation of the passed object to the console – without a line break at the end.
  • void println(Object obj) – prints the passed text or the text representation of the passed object to the console – with a line break at the end.
  • String readln(String prompt) – displays the passed prompt, accepts a user input, and returns it.

In Java 23 and Java 24, an implicitly declared class automatically imports all java.io.IO methods. This means you can call methods like println() without having to prefix them with the class name.

Starting with Java 25, the IO class is moved to the java.lang package, and the automatic import of IO methods is removed. That means from Java 25 onward, you'll either have to write IO.println() – or statically import the println() method:

import static java.lang.IO.*;Code language: Java (java)

With this import declaration, you can once again call println() and other IO methods without prefixing them with the class name.

Java main() Method Syntax

This section describes the syntax of the main method before the simplifications introduced in Java 21. Previously, a main method had to be embedded in a class, and its syntax was rigid:

public class MyMainMethodDemo
  public static void main(String[] args) {
    // code to execute
  }
  // possibly more code
}Code language: Java (java)

Only the name of the class, in the example MyMainMethodDemo, and the name of the parameter, in the example args, may be freely chosen.

If a program consists of several classes, any number of these classes may contain a main() method. To start a program, you need to specify the name of the class whose main() method you want to run, as shown at the beginning of the article.

What do the individual elements mean?

public class MyMainMethodDemo

This first line of code introduces a class in the sense of object-oriented programming. MyMainMethodDemo is the name of the class. Java code is always arranged within classes.

With compact source files and instance main methods, an explicit class definition is no longer necessary.

public static void main(String[] args)

The second line, the so-called method signature, introduces a method. Methods contain the program code to be executed.

public

public is a so-called visibility modifier. A class and the main() method it contains must be public so that the JVM can call the main() method and thus execute the program code it contains.

With compact source files and instance main methods, this is no longer necessary.

static

Object orientation differentiates between static methods and instance methods. Static methods can be called without having to create an instance of the class surrounding them – i.e., an object. Instance methods, on the other hand, can only be called on an object.

The main() method in Java must be static so that it can be called without instantiating the class – i.e. without creating an object of this class.

With compact source files and instance main methods, this is no longer necessary.

void

Methods can return values, e.g., Math.random() returns a random number. However, a main() method has no return value. And this is precisely what is indicated by the identifier void.

String[] args

This is a parameter of the method. String[] ist der Typ des Parameters: ein String-Array. And args is the name of the parameter. This name may be changed. When starting a program, so-called command line parameters can be passed, e.g., like this:

java HelloWorld.java happy coders out thereCode language: plaintext (plaintext)

These parameters are passed to the main() method as a string array and can be read and printed there, for example:

public class HelloWorld {
  public static void main(String[] args) {
    System.out.print("Hello");
    for (String arg : args) {
      System.out.print(" " + arg);
    }
    System.out.println("!");
  }
}Code language: Java (java)

If you call this program as above, you will get the following output:

Hello happy coders out there!Code language: plaintext (plaintext)

Conclusion

The main() method is the starting point of every Java program. Without this, no Java program can start. Until now, the syntax of main() methods was rigidly predefined:

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}Code language: Java (java)

In the current Java version 24 – with activated preview features – you can write the same main() method as follows:

void main() {
  println("Hello world!");
}Code language: Java (java)

This makes it easier, particularly for Java beginners, to learn the language. Concepts that only become relevant for larger programs, such as classes, the distinction between static and instance methods, visibility modifiers such as public, protected, and private, as well as coarse-grained structures such as packages and modules, can be introduced when needed.

If you liked the article, please share it using one of the share buttons at the end or leave me a comment.

Would you like to be informed when new articles are published on HappyCoders.eu? Then click here to sign up for the HappyCoders.eu newsletter.