✕ Close

Week 14 · 14.2 · Wed, Nov 4 · The Specific Skills + Try Its

Start to Stop: The Order a Java Program Runs In

Yesterday you learned the shape of a program. Today you follow one as it runs. You find where it starts, number its statements in the order they run, skip the lines that do not run, and follow it to where it stops.

Where It Starts

Big idea. A program does not start at the top of the file. It starts at main. Think of a play: the script might begin with a list of characters, but the show starts when the curtain opens. In Java, the curtain opens at main.

How to find where a program starts:

  1. Find the line that names the method main.
  2. Go to the first statement inside main's braces.
  3. That statement runs first, wherever main is in the file.

Example 1. Find the line that runs first.

1public class Main
2{
3    public static void main(String[] args)
4    {
5        System.out.println("Ready");
6        System.out.println("Set");
7    }
8}
Show the solution

Line 3 names main, and line 4 opens main's block. The first statement inside it is line 5, so line 5 runs first.

Example 2. In this program, main is below another method. Predict the output before you read on.

1public class Main
2{
3    public static void helper()
4    {
5        System.out.println("helper");
6    }
7
8    public static void main(String[] args)
9    {
10        System.out.println("first");
11        System.out.println("second");
12    }
13}
Show the solution
first
second

Many readers start at the top and write helper first. But the program starts at main, on line 8, so line 10 runs first. Nothing in main calls helper, so line 5 never runs. A method is like a recorded announcement: it is ready, but nobody hears it until main presses play.

What if main had one more statement, helper();, right before line 10? Then main would press play first, and the output would be helper, first, second.

One Statement at a Time

Big idea. Statements run in order, one at a time, like steps in a recipe. Each step finishes before the next one starts, and changing the order can change the result.

A statement can print something, or it can store a value in a box. In Java, the word int in front of a new box's name makes a box for a whole number. You learn more about int in Week 15.

You trace Java the same way you traced the steps language. The only change is the last column: in Java it is called Output, and like the shown column, it holds what each line puts on the screen.

Example 3. Number the statements in the order they run, and give the output.

1public class Main
2{
3    public static void main(String[] args)
4    {
5        int points = 2;
6        points = points + 3;
7        System.out.println(points);
8        System.out.println("done");
9    }
10}
Show the solution
line   runs   points   Output
5      1st    2
6      2nd    5
7      3rd    5        5
8      4th    5        done

Line 5 stores 2 in points. Line 6 works out 2 + 3 and stores 5 in points, replacing the 2. So when line 7 prints points, it prints 5. Line 8 prints done.

Example 4. Predict what this prints.

1int x = 4;
2x = x + 1;
3x = x * 2;
4System.out.println(x);
Show the solution

x starts at 4. The next line adds 1, so x is 5. The line after that doubles it, so x is 10, and the program prints 10.

Example 5. The same three statements, with lines 2 and 3 swapped. Predict again.

1int x = 4;
2x = x * 2;
3x = x + 1;
4System.out.println(x);
Show the solution

This time x doubles first, from 4 to 8, and then 1 is added, so it prints 9. It is like a recipe: frosting a cake before you bake it gives you a different cake.

Lines That Do Not Run

Big idea. Not every line does something. Blank lines, braces and comments are not statements, so Java skips them. A comment starts with two slashes, //, and is a note for people, like a sticky note on the recipe.

Example 6. Find the statements and the output.

1public class Main
2{
3    public static void main(String[] args)
4    {
5        // count down
6        int t = 3;
7
8        System.out.println(t);
9
10        t = t - 1;
11        System.out.println(t);
12    }
13}
Show the solution
3
2

Line 5 is a comment, and lines 7 and 9 are blank, so Java skips them. That leaves four statements: lines 6, 8, 10 and 11. Line 6 stores 3 in t. Line 8 prints 3. Line 10 takes 1 away, so t is 2. Line 11 prints 2.

Where It Stops

Big idea. After the last statement in main, the program reaches main's closing brace and stops. Nothing sends it back to the top, so no statement runs twice unless the code tells it to.

Example 7. A method is called from main. Predict the output, then number the statements in the order they run.

1public class Main
2{
3    public static void main(String[] args)
4    {
5        System.out.println("A");
6        middle();
7        System.out.println("C");
8    }
9
10    public static void middle()
11    {
12        System.out.println("B");
13    }
14}
Show the solution
A
B
C

Line 5 runs first and prints A. Line 6 calls middle, so the program jumps to middle and runs line 12, which prints B. When middle is done, the program comes back to main and runs line 7, which prints C. That was main's last statement, so the program stops at line 8. The order is 5, 6, 12, 7.

Quick checks.

- What if line 6 appeared twice, one right after the other? (B would print twice: A, B, B, C.) - What if middle were moved above main in the file? (Nothing changes. Where middle is called decides when it runs, not where it is written.)

Watch for three common mistakes on the bench. First, start at main, not at line 1. Second, a method runs only if main calls it. Third, comments, blank lines and braces are not statements.

Sources: Oracle, The Java Language Specification, Java SE 21 Edition, §12.1 and §14.2; Oracle, The Java Tutorials: Expressions, Statements, and Blocks and A Closer Look at the Hello World Application; course repository, Bench Basics B12 job data (2026).

Try It on your own

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

Try It 1. Where does every Java program start?

Try It 2. Which line runs first?

1public class Main
2{
3    public static void warmUp()
4    {
5        System.out.println("stretch");
6    }
7
8    public static void main(String[] args)
9    {
10        System.out.println("run");
11    }
12}

Try It 3. Same program. What does it print?

Try It 4. What does this print?

1int n = 3;
2n = n * 4;
3n = n - 2;
4System.out.println(n);

Try It 5. Swap lines 2 and 3 of the last program. What does it print now?

Try It 6. In what order do these statements run?

1System.out.println("one");
2int k = 2;
3System.out.println(k);

Try It 7. Which of these lines is a statement?

Try It 8. How many statements run in main?

1public class Main
2{
3    public static void main(String[] args)
4    {
5        // start
6        int score = 5;
7
8        score = score + 1;
9        System.out.println(score);
10    }
11}

Try It 9. What happens after the last statement inside main runs?

Try It 10. What does this program print?

1public class Main
2{
3    public static void main(String[] args)
4    {
5        System.out.println("open");
6        inside();
7        System.out.println("close");
8    }
9
10    public static void inside()
11    {
12        System.out.println("middle");
13    }
14}

Try It 11. In the last program, in what order do the statement lines run?