JavaMethods

Java Methods

What is a Method?

A method is a named, reusable block of code that performs a specific task. Instead of writing the same lines of code over and over wherever you need that task done, you write it once inside a method and then call the method by name whenever you need it.

Why Methods Exist
  • Reusability — write the logic once, use it as many times as you like.

  • Organization — breaking a program into small, named pieces makes it far easier to read and maintain.

  • Isolation — a method's internal details are hidden from the code that calls it; you only need to know what it does, not how.

  • Easier testing and debugging — a bug in a well-named, focused method is much easier to locate than a bug buried in one giant block of code.

Method Syntax

Java
modifier returnType methodName(parameterType parameterName) {
    // method body
    return value; // only if returnType is not void
}

Part

Meaning

modifier

Access level and other keywords, e.g. public, private, static

returnType

The type of value the method sends back, or void for none

methodName

The name you call the method by

parameters

Values the method accepts as input, listed in parentheses

body

The actual statements the method executes

A First Complete Example

Java
public class Calculator {

    // A method that takes two ints and returns their sum
    public static int add(int a, int b) {
        int sum = a + b;
        return sum;
    }

    public static void main(String[] args) {
        int result = add(5, 7);
        System.out.println("Result: " + result);
    }
}
Result: 12
Note
add(5, 7) is called from main — Java jumps into the add method, runs its body with a = 5 and b = 7, then returns 12 back to the point where it was called.
void — No Return Value

Not every method needs to hand back a result. A method that just performs an action — like printing something — uses void as its return type, meaning it returns nothing at all.

Java
public static void greet(String name) {
    System.out.println("Hello, " + name + "!");
    // no return statement needed
}

public static void main(String[] args) {
    greet("Amina");
}
Hello, Amina!
Tip
A void method CAN still use a bare return; to exit early — it just can't return a value. return 5; inside a void method is a compile error.
Calling a Method

To call (or invoke) a method, write its name followed by parentheses, supplying a value for each parameter it expects, in order.

Java
public static double square(double n) {
    return n * n;
}

public static void main(String[] args) {
    double result1 = square(4);
    double result2 = square(2.5);
    System.out.println(result1);
    System.out.println(result2);
}
16.0
6.25
Practice Exercises
  1. Write a method isEven(int n) that returns true if n is even.

  2. Write a void method printStars(int count) that prints that many asterisks on one line.

  3. Write a method max(int a, int b) that returns the larger of the two values, and call it from main.