Lesson 9 of 34 · Methods
Java Methods: Parameters and Return Values
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 frommain. (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 incamelCase:calculateTotal,printReport,isEven.(int a, int b)— the parameters: input variables with a type and a name, separated by commas.{ ... }— the body.returnsends 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
intmust return aninton 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)oradd("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 methodstatic.
Assignment
Write four static methods in Main:
square(int n)returnsn * n:square(7)is49.sumOfSquares(int a, int b)returnsa² + b²by callingsquare:sumOfSquares(3, 4)is25.isEven(int n)returnstruewhennis even (zero and negative numbers included):isEven(10)istrue,isEven(7)isfalse.greet(String name)returns nothing and prints exactly one line:greet("Ada")printsWelcome 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
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
- JavaScript Functions: Parameters and Return Values · JavaScript
- Python Functions: def, Parameters and return · Python
- Go Functions: Parameters and Return Values · Go
- C# Methods: Parameters and Return Values · C#
Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.
Lesson last updated on .