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 { } 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!");
}
}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 blockWhat are curly braces { } used for in Java?
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.
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.
The block of the main() method lies ____ the block of the class.
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 →