Almost every developer meets a new programming language through a Hello World example. Java is no exception.

A good Hello World program does more than print a greeting. It gives you a quick way to verify that the JDK is installed correctly, that your source code compiles and that the Java runtime can execute the resulting class.

Here are three simple Java Hello World examples:

  1. A standard console-based Hello World program.
  2. A graphical Hello World message with Java Swing.
  3. A Hello World message executed from a static initialization block.

1. Java Hello World with console output

The traditional Java Hello World application writes a message to standard output. This example demonstrates the basic class declaration, the main() entry point and a call to System.out.println().

package com.example.helloworld;

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

If the file is named HelloWorld.java and is stored in a directory that matches its package structure, compile it with javac and run it with java.

javac com/example/helloworld/HelloWorld.java
java com.example.helloworld.HelloWorld

2. Java Hello World with Swing

Java can also display the greeting in a graphical dialog. Swing is part of the Java desktop APIs and includes the convenient JOptionPane class.

package com.example.helloworld.swing;

import javax.swing.JOptionPane;

public class HelloWorld {

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(
            null,
            "Hello World!"
        );
    }
}

The first argument to showMessageDialog() is the parent component. Passing null tells Swing to create an independent dialog.

A package declaration is optional for a small Java example like this. It is included here because real applications normally organize classes into named packages.

3. Java Hello World with a static initialization block

A Java class can also execute code from a static initialization block. Static initialization runs when the class is initialized, before the body of main() executes.

package com.example.helloworld.initializer;

public class HelloWorld {

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

    public static void main(String[] args) {
        // The static block has already executed.
    }
}

This is not how you would normally implement application startup logic, but it demonstrates an important part of Java class initialization.

A runnable Java application should still define a valid main() method when launched in the traditional way. Static initialization blocks are intended for class initialization work, not as a replacement for the application entry point.

Java Hello World example
Three different ways to write a Hello World program in Java.

Which Java Hello World example should you use?

If you are learning Java or simply testing a JDK installation, start with the console example. It has the fewest moving parts and introduces the standard main() method used by traditional Java applications.

The Swing example is useful if you want to experiment with Java desktop development, while the static-block version is best treated as a demonstration of class initialization behavior.

Once the basic example works, you know your Java compiler and runtime are functioning correctly, and you can move on to classes, objects, collections, streams, concurrency and the rest of the Java platform.