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!
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, orvoidfor nothing). - Name β how you call it, e.g.
square(...). - Parameter(s) β the inputs it needs, listed in parentheses.
nhere is a parameter; the4you pass in when calling is the argument. returnβ hands a value back to whoever called the method. Avoidmethod never usesreturn value;(it may use a barereturn;to exit early).
Mini quiz β method anatomyβ
Method anatomy
1 / 2public 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!
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β
Calling methods and scope
1 / 2Why 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β
- A method groups reusable code under a name, so you write the logic once and call it as many times as you need.
- Its signature has a return type, a name, and zero or more parameters β the values passed in when calling are arguments.
voidmethods return nothing; every other return type must usereturn value;on every path through the method.- Parameters are local: each call gets its own fresh copies, and changes inside one method call never leak out to other variables.
- Methods in the same class can call each other directly by name β this is how you build bigger programs from small, testable pieces.
Exercisesβ
Write a method factorial(int n) that returns n! (n * (n-1) * ... * 1, and 0! = 1). Read one integer and print the result.