Skip to content

Metadata Card

  • Prerequisites: Ch4 (Control Flow)
  • Estimated time: 60 min
  • Core difficulty:
  • Reading mode: Steady progress
  • Completion marker: Can define and call methods, understand parameters vs arguments, return values, and the call stack

The Breakthrough · Tracing the Origins

You can write code that runs, but it's all in one block. As programs grow, this becomes unmanageable. Methods let you package behavior into reusable, named units.

Act 1: The Black Box

A method is a black box: you give it inputs, it produces an output. What happens inside is its own concern.

java
public class Calculator {
 public static void main(String[] args) {
 int result = add(5, 3);
 System.out.println(result); // 8
 }

 // Method definition
 public static int add(int a, int b) {
 return a + b;
 }
}

Language: Java Expected output: 8

Anatomy of a method:

  • public — access modifier (who can call it)
  • static — belongs to the class, not an instance (more on this in OOP)
  • int — return type (what it gives back)
  • add — method name
  • (int a, int b) — parameters (inputs)
  • return a + b — the body (what it does)

Parameters vs Arguments

java
public static void greet(String name, int times) {
 for (int i = 0; i < times; i++) {
 System.out.println("Hello, " + name + "!");
 }
}

// Call site:
greet("Alice", 3); // "Alice" and 3 are arguments

Expected output:

Hello, Alice!
Hello, Alice!
Hello, Alice!

name and times are parameters (the method definition). "Alice" and 3 are arguments (the actual values passed in).

Pass by Value

Java is pass-by-value. The method receives a copy of the argument.

java
public static void main(String[] args) {
 int x = 10;
 modify(x);
 System.out.println(x); // Still 10!
}

public static void modify(int value) {
 value = 99; // Only changes the local copy
}

For objects, the reference (pointer) is copied, not the object itself. So you can modify the object's contents, but you can't reassign the original reference.

The Call Stack

When a method is called, Java pushes a new "stack frame" onto the call stack. When the method returns, the frame is popped.

java
public static void main(String[] args) {
 a();
}
static void a() { b(); }
static void b() { c(); }
static void c() { System.out.println("In C"); }

Stack (from bottom to top):

main → a → b → c (top)

When c() finishes, all frames are popped. If an exception occurs, the stack trace shows this chain.

Final Challenge

  1. Write a method max(int a, int b, int c) that returns the largest of three numbers
  2. Write a method isPrime(int n) that returns true if n is prime
  3. Trace the call stack for a method that calls itself — this leads to recursion!

Traveler's Notes

Methods package behavior into named, reusable units.
Parameters are the method's input slots; arguments are the actual values.
Java passes everything by value — copies for primitives, copied references for objects.
The call stack tracks method calls — each call pushes a frame, each return pops one.

Built with VitePress | Software Systems Atlas