The Stacks
UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.10

1.10 Calling Class Methods

Class (static) methods belong to the class, not to an object. You call them with the class name, and the call transfers control until the method returns.

What you need to know

  • A class method (or static method) is declared with static and called using the class name: Math.abs(-3), Integer.parseInt("42").
  • Inside the same class, a static method can be called by name alone: helper(5).
  • A method call transfers control to the method. When it hits a return statement (or the end of a void method), control comes back to the line after the call.
  • If the method returns a value, the call expression is that value: int m = Math.max(3, 9); stores 9.
  • A void method's call is a statement by itself. Using it in an expression is a compile error.
  • Arguments are evaluated before the call; the parameter receives a copy of each argument's value.

Worked example

public class Calc
{
    public static int square(int n)
    {
        return n * n;
    }
    public static void main(String[] args)
    {
        int r = square(4) + Calc.square(2);   // 16 + 4
        System.out.println(r);                // 20
    }
}
Exam tip: Trace method calls by writing down the line you're returning to, evaluating the method with the argument values, then substituting the return value back. Multiple calls on one line are evaluated left to right.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • A call to a static method: ClassName.methodName(args). Inside the same class, the ClassName. prefix is optional. Both are common on the exam.
  • Control transfer: the call suspends the current method, runs the called method to its return, then resumes. If the called method itself calls another, that nests. A trace must track a stack of "where to return to."
  • Return substitution: the entire call expression is replaced by the returned value. 3 + max(4, 9) * 2 → 3 + 9 * 2 → 21. Evaluate the call when you reach it in normal left-to-right, precedence-respecting order.
  • Parameters get copies. For primitives, the copy is the value; the caller's variable is untouched by anything the method does. For objects (1.13, 3.6) the copy is the reference.
  • A method with void return type is called as a statement. It can still have effects — printing, modifying an object — but produces no value.
  • Early return: a return inside an if ends the method immediately. Code after it (on that path) doesn't run. A method can have several returns; exactly one executes per call.

Mistakes that cost points

  • Continuing to trace after a return. Once return executes, you're back in the caller. Stop reading the method.
  • Losing the return point. Write it down before jumping into the method.
  • Expecting a primitive argument to change. increment(x) doesn't change x. Only the returned value matters.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 Consider the following method.
public static int mystery(int a, int b)
{
    if (a > b)
    {
        return a - b;
    }
    return b - a;
}
What is printed by System.out.println(mystery(3, 8) + mystery(10, 4));?
  1. A -1
  2. B 11
  3. C 1
  4. D 17
Show answer

Answer: B. mystery(3, 8) → 8 - 3 = 5. mystery(10, 4) → 10 - 4 = 6. 5 + 6 = 11.

Q2 Which of the following correctly calls a static method display in class Printer that takes one String parameter and returns nothing?
  1. A String s = Printer.display("hi");
  2. B Printer.display("hi");
  3. C Printer p = new Printer(); display("hi");
  4. D display.Printer("hi");
Show answer

Answer: B. Static methods are called with ClassName.method(). A void method can't be assigned to a variable.

Key vocabulary

Class method / static method
a method that belongs to the class and is called with the class name
Return statement
ends the method and sends a value back to the caller