Skip to content
Java fundamentals 4 min read

Methods

A method is a named, reusable block of code that performs a task and optionally returns a value. Methods are how Java programs are organized: they reduce duplication, encapsulate logic, and define an object’s behavior. Every Java program begins execution in the special main method.

Declaring Methods

A method declaration specifies modifiers, a return type, a name, a parameter list, and a body. Use void when the method returns nothing.

public int add(int a, int b) {
    return a + b;
}

public void greet(String name) {
    System.out.println("Hello, " + name);
}

The signature add(int, int) — name plus parameter types — must be unique within a class (the return type is not part of the signature).

Parameters and Return Types

Parameters are typed inputs; the return type declares the output. A method must return a value of its declared type on every path, or the compiler complains.

public double average(int[] values) {
    int sum = 0;
    for (int v : values) {
        sum += v;
    }
    return values.length == 0 ? 0.0 : (double) sum / values.length;
}

System.out.println(average(new int[]{10, 20, 30}));  // 20.0

Output:

20.0

Tip: Keep methods short and single-purpose. A method that does one thing is easier to name, test, and reuse.

Method Overloading

Overloading defines multiple methods with the same name but different parameter lists. The compiler picks the best match by argument types — this is compile-time polymorphism.

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

System.out.println(max(3, 7));        // 7   (int version)
System.out.println(max(2.5, 1.5));    // 2.5 (double version)
System.out.println(max(1, 9, 4));     // 9   (three-arg version)

Output:

7
2.5
9

Overloads must differ in parameter count, types, or order — differing only by return type is not allowed.

Varargs

A variable-arity parameter (type...) accepts zero or more arguments, which the method receives as an array. It must be the last parameter.

public int sum(int... numbers) {
    int total = 0;
    for (int n : numbers) {
        total += n;
    }
    return total;
}

System.out.println(sum());            // 0
System.out.println(sum(1, 2, 3));     // 6
System.out.println(sum(10, 20));      // 30

Output:

0
6
30

Static vs Instance Methods

A static method belongs to the class and is called without an object. An instance method belongs to an object and can access its fields via this.

public class Calculator {
    private int memory = 0;             // instance state

    public void store(int value) {      // instance method
        this.memory = value;
    }

    public static int square(int x) {   // static method
        return x * x;
    }
}

int sq = Calculator.square(5);          // 25 — no instance needed
Calculator calc = new Calculator();
calc.store(10);                         // requires an instance

Note: A static method cannot access instance fields or this, because no particular object exists when it runs. Use static for stateless utilities.

Pass-by-Value Semantics

Java is strictly pass-by-value: a method receives a copy of each argument. For primitives, the value is copied. For objects, the reference is copied — so the method can mutate the object, but cannot make the caller’s variable point elsewhere.

static void tryReassign(int[] arr) {
    arr[0] = 99;             // mutates the shared object — visible to caller
    arr = new int[]{0};      // reassigns the local copy only — invisible
}

int[] data = {1, 2, 3};
tryReassign(data);
System.out.println(data[0]);  // 99
System.out.println(data.length); // 3 — reassignment had no effect outside

Output:

99
3

This is the single most misunderstood point about Java. The object’s contents changed (element 0), but the caller’s data reference still points to the original array.

Best Practices

  • Give methods verb-based names that describe what they do (calculateTotal, isValid).
  • Keep parameter lists short; group related arguments into an object when they grow.
  • Prefer overloading over flag parameters for related behaviors.
  • Use static for pure, stateless utility functions; instance methods for behavior tied to object state.
  • Validate arguments early and fail fast with clear exceptions.

Interview Questions

Q: Is Java pass-by-value or pass-by-reference? A: Always pass-by-value. For objects, the reference is passed by value — the method can mutate the object but cannot change which object the caller’s variable refers to.

Q: What is the difference between overloading and overriding? A: Overloading is multiple methods with the same name but different parameters in the same class, resolved at compile time. Overriding is a subclass redefining a superclass method with the same signature, resolved at runtime (dynamic dispatch).

Q: Can a static method be overridden? A: No. Static methods are bound to the class, not an instance. A subclass can declare a static method with the same signature, but this is hiding, not overriding — there is no runtime polymorphism.

Last updated June 1, 2026
Was this helpful?