W3docs

Java Methods

Define and call methods in Java — return types, parameters, and the method body — to organize reusable code.

A method in Java is a named block of code that you can call from elsewhere in the program. Methods are how you avoid repetition: instead of writing the same calculation or the same five-line operation in three places, you put it in a method and call it three times.

You've already seen one method on every page of this book — main. Every Java program is just a class with at least one method that the JVM knows how to start. In this part of the book we'll look at how to write your own.

Anatomy of a method

A method declaration has up to five parts: modifiers, a return type, a name, a parameter list, and a body.

public static int square(int n) {
  return n * n;
}
  • public static — modifiers. public controls visibility; static means the method belongs to the class rather than to an instance. We'll dig into both in later parts; for now, every method you write in a top-level utility class will be public static.
  • int — the return type. The kind of value the method hands back to its caller. Use void if it returns nothing.
  • square — the name. Conventionally lowerCamelCase, and a verb or verb phrase that says what the method does.
  • (int n) — the parameter list. Inputs the caller must supply. Each parameter is a type and a name.
  • { ... } — the body. The code that runs when the method is called.

Calling a method

Once defined, you call a method by writing its name followed by (...) with the argument values:

int result = square(7);   // result is 49
System.out.println(square(3) + square(4));   // 9 + 16 = 25

The value the method returns can be used like any other expression — assigned to a variable, passed as an argument, printed, added to something else.

The return statement

return does two things: it stops the method immediately and sends a value back to the caller.

public static int absolute(int n) {
  if (n < 0) {
    return -n;
  }
  return n;
}

Once return -n runs, the method is over — the second return n doesn't execute. A method with a non-void return type must return a value on every possible code path; the compiler refuses to compile if a path is missing.

For void methods, you can use a bare return; to exit early, but you don't have to write one at the end:

public static void greet(String name) {
  if (name == null) return;          // early exit
  System.out.println("Hello, " + name);
}                                    // implicit return at the closing brace

Parameters and arguments

The names in the method's signature are parameters; the values you pass at the call site are arguments. The distinction is small but worth getting right:

public static int sum(int a, int b) {   // a, b are parameters
  return a + b;
}

int x = 3, y = 4;
int total = sum(x, y);                   // x, y are arguments

Each call gets its own fresh copies of the parameters. Assigning to a inside sum does not change x at the call site. The chapter on pass-by-value digs into the details.

Return type rules

The expression after return must be assignment-compatible with the declared return type — same type, or a type Java can widen to it:

public static double half(int n) {
  return n / 2.0;       // int / double → double, fine
}

public static int half(int n) {
  return n / 2.0;       // ERROR: double cannot be implicitly narrowed to int
}

If you really need to narrow, you cast explicitly: return (int)(n / 2.0);.

Methods can call other methods

A method's body is just regular code — including calls to other methods, including itself (see recursion):

public static int square(int n)   { return n * n; }
public static int sumOfSquares(int a, int b) {
  return square(a) + square(b);
}

Building small, single-purpose methods and composing them is the heart of writing readable Java.

Returning is not printing

A common beginner mix-up: a method that prints a value and a method that returns one are not interchangeable. return hands a value back so the caller can use it; System.out.println only writes text to the console and the caller gets nothing.

public static void printSquare(int n) {
  System.out.println(n * n);   // shows the result, returns void
}

public static int square(int n) {
  return n * n;                // gives the result back to the caller
}

You can store, add, or pass on the result of square, but int x = printSquare(5); won't compile — printSquare returns void. Prefer returning a value and let the caller decide whether to print it; it keeps the method reusable.

Overloading: same name, different parameters

Java lets two or more methods share a name as long as their parameter lists differ — in the number of parameters or their types. This is called overloading, and the compiler picks the right one based on the arguments you pass:

public static int max(int a, int b)          { return a > b ? a : b; }
public static double max(double a, double b)  { return a > b ? a : b; }
public static int max(int a, int b, int c)    { return max(max(a, b), c); }

max(3, 9) calls the first, max(2.5, 1.5) the second, and max(4, 1, 7) the third. The return type alone is not enough to distinguish overloads — only the parameters count. The method overloading chapter covers the matching rules in full.

Where methods live

A method always lives inside a class. You can't write a top-level function the way you can in JavaScript or Python. The class is the container; the method is one of its members. So a complete file with two methods looks like this:

public class MathUtils {
  public static int square(int n) {
    return n * n;
  }

  public static int cube(int n) {
    return n * n * n;
  }
}

From main in the same class, you'd call them as square(5) and cube(2). From a different class, you'd qualify them: MathUtils.square(5).

A worked example

java— editable, runs on the server

What's next

Now that you can declare and call a method, the next question is how data flows into it. The parameters chapter goes through the details — how arguments line up with parameters, what types are allowed, and what happens when the caller passes objects versus primitives.

Practice

Practice
Which statement about a Java method's return type is true?
Which statement about a Java method's return type is true?
Was this page helpful?