Show students many examples so they see that Java passes arguments by value to help improve student understanding of scope.

  • Below is example code you can show to students.
  • Students might expect that the variable x is changed by being passed to the method foo.

public class Example {
    public static void main(String[] args) {
        int x = 10;
        Example.foo(x);
        System.out.println(x); // prints 10
    }
    public static void foo(int y) {
        y = y + 3;
        System.out.println(y); // prints 13
    }
}

More about this tip

External Source

Interview with Richard Weiss.