Scope and pass-by-value
What you will learn
Scope determines where a name can be used. Java also passes every argument by value: a method receives its own parameter variable, even if the value is a reference to an existing object.
public class Main { static void change(int count) { count = 9; System.out.println(count); } public static void main(String[] args) { int count = 2; change(count); System.out.println(count); }}Expected output:
92The parameter changes; the caller variable remains two.
How it works
A parameter name is usable in the method body; an ordinary local variable declared in a block is in scope from its declaration through the rest of that block (including its initializer, subject to definite assignment). Each invocation has its own local variables. A primitive argument copies the primitive value. For an array argument, Java copies the reference value, so both variables initially refer to the same array. Assigning a different array to the parameter changes only that parameter. Assigning an element changes the shared array object and is visible through the caller reference. Arrays were previewed in the loops chapter: int[] is an array reference type and a[0] accesses its first element. The full array lesson comes next. A final parameter cannot be reassigned, but final alone does not make the referenced object immutable.
public class Main { static void adjust(int[] stock) { stock[0] = 8; stock = new int[] {99}; System.out.println(stock[0]); } public static void main(String[] args) { int[] stock = {3}; adjust(stock); System.out.println(stock[0]); }}Expected output:
998The first update changes the shared array. The later assignment points only the parameter to a new array.
Check a boundary or unusual input
public class Main { static int calculate(int base) { int result = base + 1; return result; } public static void main(String[] args) { { int result = calculate(4); System.out.println(result); } { int result = calculate(9); System.out.println(result); } }}Expected output:
510The two result variables have disjoint scopes; neither is the local variable inside calculate.
Common mistakes
Java does not pass caller variables by reference. Mutation through a copied reference is possible but reassignment does not replace the caller variable. Avoid redeclaring a parameter or an enclosing local variable inside a nested block; Java rejects that overlapping declaration. Return a replacement value when you want the caller to choose to update its own variable.
Try it yourself
Write static int increment(int stock) returning stock + 1 without mutating any object. In main, print the result and the original stock separately. Test stock 4, 0 and 10; inputs 0–10.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Scope and pass-by-value — knowledge check
Apply scope and pass-by-value to examples and boundary conditions.
How are Java arguments passed?
A method assigns 9 to its int parameter. What happens to the caller int variable?
A method writes a[0] = 8 through a copied array reference. What can the caller observe?
A method sets its array parameter to a new array. What happens to the caller reference?
Can a local declared inside an ordinary if block be named outside it?