Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Java Program Structure · Lesson 14 of 28

Code Blocks

A free lesson from Swipe Coding · Java, level 1 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Curly braces are how Java knows where a group of code starts and ends. 🧱

Curly braces { } group multiple statements together into a shared block in Java. A block marks a clear beginning and a clear end — for example the body of a class or the body of a method.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}
The outer curly braces belong to the class "Main", the inner ones to the main() method.
🤖
See how one block can live neatly inside another. 📦

Blocks can be nested inside one another, similar to a box inside another box. The block of the main() method lies completely inside the block of the class — it starts later and ends earlier than the class block.

public class Main { // Start of class block
    public static void main(String[] args) { // Start of method block
        System.out.println("Hello, Java!");
    } // End of method block
} // End of class block
Marked with comments, you can clearly see: the method block lies entirely within the class block.
Quick check

What are curly braces { } used for in Java?

🤖
Miss a closing brace and the compiler will let you know. ⚠️

Every opening brace { must eventually have a matching closing brace }. If one is missing, the compiler can't tell where a block ends and reports an error — even if the rest of the code is correct.

True or false

Every opening curly brace { needs a matching closing brace }.

As a reminder: the class itself is a block, the main() method is another block inside it, and the individual statements from the last lesson sit inside these blocks. That's how everything in a Java program fits together.

Fill in the blank

The block of the main() method lies ____ the block of the class.

True or false

You can omit curly braces { } if the code is short enough.

Keep your streak going in Swipe Coding

Swipe through every level of Java on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →