Methods

The Course
•
4 min read

A method is a named piece of work. You have already called println and length; now you will define your own operations. Defining a method does not run its body. A call, with parentheses after the name, tells Java to execute that method. When it finishes, execution returns to the statement after the call.

In today’s classic Main program, helper methods use static so main can call them directly. A static method belongs to the class rather than to one object. We will contrast instance methods on Day 5. void means the method does not return a value to its caller. It may still print or perform other work.

Keep definitions beside main

Methods are declared inside the class, alongside main, not inside main’s body. A closing brace ends each method, and the final brace ends the class. Blank lines between methods make these boundaries easier to read. Indentation is for people; the braces determine the structure Java compiles.

Choose a name describing the work: printHeader is clearer than doStuff. Extract a method when it represents a meaningful operation, not simply because there are two lines. A method called from several places makes one change apply to every call, reducing the chance that copied versions drift apart.

Define once, call twice Java
public class Main {
    public static void main(String[] args) {
        printHeader();
        System.out.println("Java");
        printHeader();
    }

    static void printHeader() {
        System.out.println("Study plan");
        System.out.println("----------");
    }
}
Check your prediction
Study plan
----------
Java
Study plan
----------

Java enters main first, even though the helper is written below it. Each call runs the same body. Remove both calls: the definition remains valid, but its output disappears.

Follow calls back to their caller Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Before");
        printGoal();
        System.out.println("After");
    }

    static void printGoal() {
        System.out.println("Practise every day");
    }
}
Check your prediction
Before
Practise every day
After

The helper finishes before main proceeds. Draw an arrow from the call to the helper, then another back to the next statement. Definitions can be below the place where they are called.

Compiler error: call a name that does not exist Java
public class Main {
    public static void main(String[] args) {
        printGoall();
    }

    static void printGoal() {
        System.out.println("Practise");
    }
}
Read the diagnostic

The extra l changes the method name. Java reports cannot-find-symbol because no matching method exists. Compare the name in the call with the declaration before changing the method body.

Your turn

Exercise

Print a reusable separator

Define static void printSeparator() to print ===. Call it before and after a message containing Java practice. The output must be ===, Java practice, === on separate lines. Do not copy the separator println into main twice.

Your workspace Java
public class Main {
    public static void main(String[] args) {
        // Call your helper around the message.
    }

    // Define the helper alongside main.
}
Hint 1: the idea

The repeated operation belongs in a helper; main decides when that operation happens.

Hint 2: a first step

Write a void method inside Main but outside main, then call it with an empty pair of parentheses.

Show one solution
public class Main {
    public static void main(String[] args) {
        printSeparator();
        System.out.println("Java practice");
        printSeparator();
    }

    static void printSeparator() {
        System.out.println("===");
    }
}
===
Java practice
===

Change it and explain

Change the separator from === to --- in just one place. Then call the helper inside a loop that runs three times and predict how many lines it prints. A loop repeats a call; a method definition alone does not repeat or execute.

Read the braces aloud: class starts, main starts, main ends, helper starts, helper ends, class ends. If a compiler reports an illegal start near your helper, check whether you accidentally placed its declaration inside main. Repair the structure before trying to repair unrelated lines further down.

Was this article helpful?

Your response is saved on this device.