Skip to main content

Chapter 9a - Methods

Why Write a Method?​

So far every line of your programs has lived inside main. A method is a named, reusable block of code that you can call whenever you need it β€” instead of copy-pasting the same lines again and again.

public class Main {

// a method: return type, name, parameters
public static int square(int n) {
return n * n;
}

public static void main(String[] args) {
System.out.println(square(4)); // 16
System.out.println(square(7)); // 49
int x = square(3);
System.out.println(x); // 9
}
}

πŸ§ͺ Try the code out!

Anatomy of a method
public static int square(int n) {
// ^^^^^^ ^^^^^^ ^^^^^^ ^
// return name parameter
// type
return n * n;
}
  • Return type β€” what kind of value comes back out (int, double, String, or void for nothing).
  • Name β€” how you call it, e.g. square(...).
  • Parameter(s) β€” the inputs it needs, listed in parentheses. n here is a parameter; the 4 you pass in when calling is the argument.
  • return β€” hands a value back to whoever called the method. A void method never uses return value; (it may use a bare return; to exit early).

Mini quiz β€” method anatomy​

Quiz

Method anatomy

1 / 2

public static int square(int n) { return n * n; } β€” what is n called here?

Pick an answer to see if you are right.


Calling Methods From Each Other​

Methods can call other methods, which is how larger programs stay organized into small, testable pieces.

public class Main {
public static boolean isEven(int n) {
return n % 2 == 0;
}

public static String describe(int n) {
if (isEven(n)) {
return n + " is even";
} else {
return n + " is odd";
}
}

public static void main(String[] args) {
System.out.println(describe(4));
System.out.println(describe(7));
}
}

πŸ§ͺ Try the code out!

Parameters are local

A parameter only exists inside its own method. Changing n inside square never affects any variable named n somewhere else β€” each method call gets its own fresh copy.


Mini quiz β€” calling and scope​

Quiz

Calling methods and scope

1 / 2

Why can describe(int n) call isEven(n) directly, without importing anything?

Pick an answer to see if you are right.


Challenge: isEven​

Write a method isEven(int n) that returns true if n is even, false otherwise. Read one integer and print the result.


Challenge: max of two​

Write a method maxOf(int a, int b) that returns the larger of the two. Read two integers and print the result.


Chapter summary​

Key takeaways
  1. A method groups reusable code under a name, so you write the logic once and call it as many times as you need.
  2. Its signature has a return type, a name, and zero or more parameters β€” the values passed in when calling are arguments.
  3. void methods return nothing; every other return type must use return value; on every path through the method.
  4. Parameters are local: each call gets its own fresh copies, and changes inside one method call never leak out to other variables.
  5. Methods in the same class can call each other directly by name β€” this is how you build bigger programs from small, testable pieces.

Exercises​

Activity: Factorial

Write a method factorial(int n) that returns n! (n * (n-1) * ... * 1, and 0! = 1). Read one integer and print the result.