Java Methods
A method is a named block of code that does one job. You have been calling methods all along: println, length, Math.max. Now you will write your own. Methods let you give a name to a piece of work, reuse it as often as you like, and split a long program into small parts that are easy to understand and test.
Defining and calling a method
A method is written inside the class, next to main. For now every method starts with static; the Class Methods topic explains what that word means.
public class Main {
static void greet() {
System.out.println("Hello!");
System.out.println("Welcome to Java.");
}
public static void main(String[] args) {
greet();
greet();
}
}Hello! Welcome to Java. Hello! Welcome to Java.
static void greet()defines a method calledgreet.voidmeans it gives nothing back.- The empty brackets mean it needs no input.
greet();calls the method: Java runs its block, then comes back to the line after the call.
The order of methods in the class does not matter; main is always where the program starts.
Parameters: giving a method input
Inside the brackets you list parameters, variables that receive values from the call:
public class Main {
static void greet(String name, int times) {
for (int i = 0; i < times; i++) {
System.out.println("Hello, " + name + "!");
}
}
public static void main(String[] args) {
greet("Anil", 2);
greet("Sara", 1);
}
}Hello, Anil! Hello, Anil! Hello, Sara!
The values in the call ("Anil", 2) are called arguments. They are matched to the parameters by position. The next topic, Method Parameters, goes deeper.
Returning a value
Instead of void, a method can declare a return type and give a value back with return. The call can then be used like a value:
public class Main {
static int square(int x) {
return x * x;
}
static double circleArea(double radius) {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
int s = square(9);
System.out.println(s);
System.out.println(square(3) + square(4));
System.out.printf("%.2f%n", circleArea(2));
}
}81 25 12.57
return ends the method immediately. A method with a return type must return a value on every path, or it does not compile.
Methods that decide
Methods often return a boolean, which makes conditions read like sentences:
public class Main {
static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int d = 2; d * d <= n; d++) {
if (n % d == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
for (int n = 1; n <= 30; n++) {
if (isPrime(n)) {
System.out.print(n + " ");
}
}
System.out.println();
}
}2 3 5 7 11 13 17 19 23 29
The main method now reads almost like English: for every n, if n is prime, print it. That is the real value of methods: they hide the details behind a good name.
Practice exercises
- Write a method
printLine(int length)that prints a line of that many dashes. - Write a method
max3(int a, int b, int c)that returns the largest of three numbers. - Write a method
isEven(int n)and use it to print the even numbers from 1 to 20.