Skip to content
Codewilds

Lesson 9 of 34 · Methods

Java Methods: Parameters and Return Values

easy10 XP3 min read

So far all your code has lived inside main or inside methods we gave you. Real programs are built from many small methods — named blocks of code that each do one job. Writing your own methods is how you stop repeating yourself and how you turn a long, tangled program into readable steps.

Anatomy of a method

static int add(int a, int b) {
    return a + b;
}

Reading from left to right:

  • static — the method belongs to the class itself, so you can call it directly from main. (Chapter 4 explains non-static methods.)
  • int — the return type: the kind of value the method gives back.
  • add — the name. Method names are verbs in camelCase: calculateTotal, printReport, isEven.
  • (int a, int b) — the parameters: input variables with a type and a name, separated by commas.
  • { ... } — the body. return sends a value back to the caller.

Calling a method

You call a method by its name, passing arguments in parentheses. The arguments are copied into the parameters, in order:

int total = add(2, 3);              // a = 2, b = 3 → total is 5
System.out.println(add(total, 10)); // 15

A call is an expression, so its result can be stored in a variable, printed, or passed straight into another call.

void methods and return

A method that does something but returns nothing has the return type void:

static void greet(String name) {
    System.out.println("Welcome, " + name + "!");
}

// somewhere in main:
greet("Ada");
Welcome, Ada!

A void method is called as a statement on its own. Trying to use it as a value — String s = greet("Ada"); — does not compile, because there is no value to store.

return immediately ends a method — every path through a non-void method must end with one, or the compiler reports missing return statement. Returning early often makes code simpler:

static String sign(int n) {
    if (n > 0) {
        return "positive";
    }
    if (n < 0) {
        return "negative";
    }
    return "zero";
}

Pass-by-value and scope

Java passes arguments by value: the method receives a copy. Changing a parameter never changes the caller's variable:

static void addTen(int x) {
    x = x + 10;   // changes only the local copy
}

int score = 5;
addTen(score);
System.out.println(score);   // still 5

If you want a result, return it: score = plusTen(score);.

Variables declared inside a method (including its parameters) are local: they exist only while the method runs and are invisible to other methods. Two methods can each have their own x without interfering.

Methods calling methods

Small methods combine into bigger ones:

static int square(int n) {
    return n * n;
}

static int sumOfSquares(int a, int b) {
    return square(a) + square(b);
}

Each method is easy to read and test on its own, and a bug fixed in square is fixed everywhere it is used.

Common mistakes

  • Missing return: a method declared to return int must return an int on every path.
  • Ignoring the result: add(2, 3); on its own line computes 5 and throws it away. Store or use the returned value.
  • Wrong arguments: calling add(2) or add("2", 3) does not compile — the arguments must match the parameters in number and type.
  • Expecting a parameter to change the caller's variable: return the new value instead.
  • Calling a non-static method from main: in this chapter, mark every method static.

Assignment

Write four static methods in Main:

  1. square(int n) returns n * n: square(7) is 49.
  2. sumOfSquares(int a, int b) returns a² + b² by calling square: sumOfSquares(3, 4) is 25.
  3. isEven(int n) returns true when n is even (zero and negative numbers included): isEven(10) is true, isEven(7) is false.
  4. greet(String name) returns nothing and prints exactly one line: greet("Ada") prints Welcome to the Highlands, Ada!.

Premium lesson

Unlock the full Learn Java course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Functions and methods in other languages

Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.

Lesson last updated on .

Write code ↓
Java+10 XP

Lesson 9 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn Java course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.