✕ Close

Week 14 · 14.1 · Mon, Nov 2 · The Specific Skills + Try Its

The Shape of a Java Program

Today you learn to read the shape of a Java program. By the end, you can name its parts, match each brace with its partner, count its statements, and find where it starts. Those are the four things today's bench asks you to do.

One Program, Line by Line

Big idea. A Java program is built like nesting boxes. The class is the big box, main is a box inside it, and the statements are the things packed inside main.

Every Java program you read this semester has the same outer shape. On the outside is a class. Inside the class is a method named main. Inside main are the statements, the lines that do the work. The output under each program here is what the program really printed when we ran it.

Example 1. Name the parts of this program.

1public class Main {
2    public static void main(String[] args) {
3        System.out.println("Bench Basics");
4        System.out.println("The Shape");
5        System.out.println("Every statement ends with a semicolon.");
6    }
7}
Show the solution
Bench Basics
The Shape
Every statement ends with a semicolon.

Read it from the outside in. Line 1 names the class, Main, and its brace opens the class. Line 2 starts the method main, and its brace opens main. Lines 3, 4 and 5 are three statements. Each one prints one line, which is why the output has three lines. Then the braces close in reverse order: line 6 closes main, and line 7 closes the class.

Example 2. List every part of Example 1 with its line.

Show the solution
part                          line   what it does
class header                  1      names the class Main and opens its brace
header of main                2      names the method main and opens its brace
statement                     3      prints Bench Basics
statement                     4      prints The Shape
statement                     5      prints Every statement ends with a semicolon.
brace that closes main        6      ends the block of main
brace that closes the class   7      ends the class

You will see the words public, static and void in front of main in every program, with String[] args inside its parentheses. You learn what those words mean later, in AP Computer Science A. For now, your job is only to find each part.

Braces Come in Pairs

Big idea. Braces work like parentheses in math: every opening one needs a closing partner, and the inner pair closes first.

An opening brace, {, starts a block, and its matching closing brace, }, ends it. A block is the group of lines between one pair of braces. Pairs can sit inside other pairs, like boxes inside boxes. The inner pair always closes first.

Example 3. On the bench, each brace sits on a line of its own. Match each brace with its partner.

1public class Greeting
2{
3    public static void main(String[] args)
4    {
5        System.out.println("Hello");
6        System.out.println("Who dat");
7    }
8}
Show the solution
brace on line 2   opens the class    its partner is line 8
brace on line 4   opens main         its partner is line 7

The pair on lines 4 and 7 sits inside the pair on lines 2 and 8. The inner pair closes first, so line 7 closes main. The last brace, on line 8, closes the class.

Example 4. A program left out the class's last brace. Describe what happens.

Show the solution
error: reached end of file while parsing

The program does not run at all. Java read to the end of the file and the class was still open, because the class's opening brace never found its partner.

Statements and Semicolons

Big idea. A semicolon is Java's period. Count the periods and you have counted the sentences; count the semicolons and you have counted the statements.

A statement is one instruction, a little like one sentence in English. Just as a sentence ends with a period, a statement ends with a semicolon. So you count statements by their semicolons, not by their lines.

Example 5. Count the statements in main.

1public class Lineup {
2    public static void main(String[] args) {
3        System.out.println("Doors open"); System.out.println("Find a seat");
4        System.out.println("Show starts");
5    }
6}
Show the solution
Doors open
Find a seat
Show starts

Line 3 has two semicolons, so it holds two statements. Line 4 has one. That makes three statements, and the output has three lines.

Example 6. A program left the semicolon off the first of two println lines. Describe what happens.

Show the solution
error: ';' expected

The program does not run. Java says it expected a semicolon at the end of the statement, and it prints nothing, not even the line that was right.

Where a Program Starts

Big idea. A program does not start at the top of the file. It starts at main, the way a show starts when the curtain opens, not at the cast list.

A program can hold more than one method, but it always starts at main, wherever main is in the file. Then the statements in main run from top to bottom, one at a time.

Example 7. This program has a method named extra above main. Find what it prints.

1public class Parade
2{
3    static void extra()
4    {
5        System.out.println("Never shown");
6    }
7
8    public static void main(String[] args)
9    {
10        System.out.println("Floats line up");
11        System.out.println("Bands march");
12    }
13}
Show the solution
Floats line up
Bands march

Many readers start at the top and write Never shown first. But extra comes first only in the file: the program starts at main, on line 8, so line 10 runs first. Nothing in main uses extra, so the line inside extra never runs, and Never shown never appears.

Quick checks.

- What if main had one more statement, extra();, right before line 10? (Then Never shown would print first.) - What if extra were moved below main in the file? (Nothing changes. A method runs only when main calls it, wherever it is written.)

Watch for three common mistakes on the bench. First, the last brace closes the class, not main. Second, count semicolons, not lines. Third, start at main, not at the top of the file.

Sources: Oracle, The Java Tutorials: A Closer Look at the Hello World Application and Expressions, Statements, and Blocks; course repository, The day's Java programs, compiled and run with the course JDK and Bench Basics B11 job data (2026).

Try It on your own

Work each one in your notebook first, then check it.

Try It 1. What is the name of this program's class?

1public class Weather {
2    public static void main(String[] args) {
3        System.out.println("Sunny");
4        System.out.println("High of 78");
5    }
6}

Try It 2. In the Weather program above, which line is the header of main?

Try It 3. The Weather program above has six lines. Which lines are statements, and what does the program print?

Check your answer

Lines 3 and 4 are the statements, because each one ends with a semicolon. The program prints Sunny, then High of 78, each on its own line.

Try It 4. Which line holds the brace that closes main?

1public class Tickets
2{
3    public static void main(String[] args)
4    {
5        System.out.println("Row 4");
6        System.out.println("Seat 12");
7        System.out.println("Enjoy the show");
8    }
9}

Try It 5. A program leaves out the last brace of its class. What happens?

Try It 6. How many statements does main hold?

1public class Recess {
2    public static void main(String[] args) {
3        System.out.println("Bell rings"); System.out.println("Line up");
4        System.out.println("Go outside"); System.out.println("Play");
5    }
6}

Try It 7. A program's line 3 reads System.out.println("Hi") with no semicolon at the end. What happens?

Try It 8. What is printed first?

1public class Practice
2{
3    static void warmUp()
4    {
5        System.out.println("Stretch");
6    }
7
8    public static void main(String[] args)
9    {
10        System.out.println("Team huddle");
11        System.out.println("Run drills");
12    }
13}

Try It 9. Write exactly what the Practice program above prints, one line for each line of output.

Check your answer

Team huddle, then Run drills. The program starts at main, named on line 8, and runs its two statements, lines 10 and 11, from top to bottom. Nothing in main uses warmUp, so Stretch is never printed.