Skip to content
  • Methods & Pass-by-Value · Java

Methods & Pass-by-Value

LearnViaNotes @learnvianotes· 3 min read

Java is always pass-by-value. For objects, the value passed is a copy of the reference — so you can mutate the object, but never reseat the caller's variable.

Anatomy of a method

Java
public class Main {
    //  modifiers   return type  name   parameters        throws clause
    public static   int          add(   int a, int b)    /* throws … */ {
        return a + b;
    }

    static void greet(String name) {           // void: returns nothing
        if (name == null) return;              // early return
        System.out.println("Hi " + name);
    }

    public static void main(String[] args) {
        System.out.println(add(2, 3));
        greet("Asha");
        greet(null);
    }
}
Output
5
Hi Asha
PartMeaning
SignatureName + parameter types (e.g. add(int, int)). Return type and parameter names are not part of it.
Access modifierpublic, protected, package-private (none), private
staticBelongs to the class — call as Main.add(…) without an object
Return typeAny type or void; every path of a non-void method must return or throw
ParametersLocal variables initialized with copies of the arguments

Pass-by-value, demonstrated

Java
import java.util.*;

public class Main {
    static void bump(int x) { x++; }                          // changes the copy only

    static void mutate(List<String> list) { list.add("added"); }   // same object → visible

    static void reassign(List<String> list) {
        list = new ArrayList<>();                             // local copy now points elsewhere
        list.add("lost");
    }

    static void swap(Integer a, Integer b) { Integer t = a; a = b; b = t; }

    public static void main(String[] args) {
        int n = 5;
        bump(n);
        System.out.println(n);

        List<String> items = new ArrayList<>(List.of("x"));
        mutate(items);
        reassign(items);
        System.out.println(items);

        Integer p = 1, q = 2;
        swap(p, q);
        System.out.println(p + " " + q);                      // swap is impossible this way
    }
}
Output
5
[x, added]
1 2

caller: items → [list object] → call copies the reference → param list → same [list object] → list = new … rebinds only the param

Return values & multiple results

Java
public class Main {
    record MinMax(int min, int max) {}                 // return several values via a record

    static MinMax range(int... xs) {
        int lo = Integer.MAX_VALUE, hi = Integer.MIN_VALUE;
        for (int x : xs) { lo = Math.min(lo, x); hi = Math.max(hi, x); }
        return new MinMax(lo, hi);
    }

    public static void main(String[] args) {
        MinMax r = range(4, -2, 9, 0);
        System.out.println(r + " -> " + r.min() + ".." + r.max());
    }
}
Output
MinMax[min=-2, max=9] -> -2..9

Local variables & scope

  • Local variables have no default value — using one before assignment is a compile error (fields do get defaults).

  • A variable's scope is its enclosing block { … }; you can't redeclare a name that is already in scope in the same method.

  • var (Java 10) infers a local's type: var list = new ArrayList<String>(); — locals only, must be initialized.

  • Parameters can be final to document that they're not reassigned.

Java
public class Main {
    public static void main(String[] args) {
        int x;
        System.out.println(x);       // error: variable x might not have been initialized
    }
}

Is Java pass-by-value or pass-by-reference?

Can two methods differ only by return type?

What does static mean for a method?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report