Lesson 3 of 614 minModule progress 0%

Module 1: Start Coding in Java

How Your First Java Program Works

Understand the four parts of the program you just ran: the class, the `main` method, braces, and the statement that prints output.

Author

Java Learner Editorial Team

Reviewer

Technical review by Java Learner

Last reviewed

2026-07-13

Java version

Java 25 LTS

Learning goals

  • Identify where the class and `main` method begin and end
  • Explain where Java starts running a simple program
  • Write another output statement in the correct place

Before you start

  • You have run and changed at least one browser compiler example

1. public class Main is the program container. Beginner Java code is written inside a class. This class is named Main, which is also the file name used by the browser compiler: Main.java.

2. main is where execution begins. When Java starts this program, it enters public static void main(String[] args). You do not need to memorize every keyword today; recognize the whole line as the starting point.

3. Braces mark what belongs inside. The outer { } pair belongs to the class. The inner pair belongs to main. Statements that should run at startup go between the inner braces.

4. System.out.println produces output. The text goes between double quotes, and the statement ends with a semicolon. println moves to a new line after printing, so two calls produce two output lines.

Runnable examples

Read the structure from the outside in

public class Main {                 // Class starts here
    public static void main(String[] args) { // Program starts here
        System.out.println("Hello, Java!");  // Statement
    }                                        // main ends here
}                                            // Class ends here

Expected output

Hello, Java!

Statements run from top to bottom

public class Main {
    public static void main(String[] args) {
        System.out.println("First: read the code");
        System.out.println("Second: predict the output");
        System.out.println("Third: run the program");
    }
}

Expected output

First: read the code
Second: predict the output
Third: run the program

Common mistakes

Placing a print statement outside the `main` method

For now, put executable statements between the two braces that belong to `main`.

Using single quotes for a sentence

Use double quotes for text such as `"Hello"`. Single quotes are for one character, which you will learn later.

Losing track of a closing brace

Indent the code as shown. Matching braces at the same indentation level are easier to spot.

Mini exercise

Add a fourth `println` statement to the second example. Before running it, point out which brace closes `main` and which brace closes the class.

Summary

  • The `Main` class contains the beginner program.
  • Java starts a simple program inside the `main` method.
  • Braces define boundaries, and semicolons finish statements.
  • `System.out.println` prints one line of output.

Next step

You can now read the structure instead of treating it as magic. Next, install JDK 25 so the same program can run on your own computer.

Sources used

Advertisement

Lesson check

Where should a beginner place a statement that must run when the program starts?

Next lesson →