0

As far as I know, when we pass an object reference to a method, changes we made to that reference inside the method directly affects the object. When I try to make the object reference null, it seems that the object reference is not set to null and I can still access the object information. Could you point out what causes this and why the object reference doesn't become null. Thanks. (My code is shown below)

public class Main {
    public static void main(String[] args) {
        Circle c1 = new Circle(5.8);
        foo(c1);
        System.out.println(c1);
    }

    public static void foo(Circle c){
        c = null;
    }
}
Mumumu
  • 1
  • 2
  • 1
    *changes we made to that reference inside the method directly affects the object.* Nope, changing a reference never affects the object. – shmosel Jun 14 '17 at 20:03
  • 2
    You have to understand that a **variable** and the object that the variable points two are two separate things. When you change the **variable** (make it point somewhere else, or make it `null`), nothing happens to the object, nor to any other variable that points to the same object. Java is pass-by-value only; see the linked question. – Jesper Jun 14 '17 at 20:04
  • Thanks guys, now I understand the point I missed. – Mumumu Jun 14 '17 at 20:11

0 Answers0