Methods that call methods
Any method can call other methods, not only main. A bigger method can then be built from smaller ones that each do one thing:
public class Main {
public static void main(String[] args) {
System.out.println(distance(3, 4));
}
public static int square(int number) {
return number * number;
}
public static double distance(int x, int y) {
return Math.sqrt(square(x) + square(y));
}
}
5.0distance calls square twice and uses what it returns. Each small method can be checked on its own, and once square works, every method that uses it can rely on it.
Methods that print can call each other too. A method that prints a table can call a method that prints one row, once for each row. The row method then only has to know how to print a single row.