-2

Should'nt p.name = "EJava"; being altered after method anotherMethod(p); was invoked ? Can anyone explain why this code returns :

anotherMethod
EJava
someMethod
someMethod

instead of :

anotherMethod
anotherMethod
someMethod
someMethod

Class Person

class Person {
   public String name;
   public int height;
}    

Tester

class EJavaGuruPassObjects1 {
    public static void main(String args[]) {
        Person p = new Person();
        p.name = "EJava";
        anotherMethod(p);
        System.out.println(p.name);
        someMethod(p);
        System.out.println(p.name);
    }

    static void someMethod(Person p) {
        p.name = "someMethod";
        System.out.println(p.name);
    }

    static void anotherMethod(Person p) {
        p = new Person();
        p.name = "anotherMethod";
        System.out.println(p.name);
    }
}

Should'nt p.name = "EJava"; being altered after method anotherMethod(p); was invoked?

Alex K
  • 22,315
  • 19
  • 108
  • 236
George
  • 1
  • 1
  • 1
  • 4

4 Answers4

3

You are creating a new Person object in anotherMethod().It should be

static void anotherMethod(Person p) {
   p.name = "anotherMethod";
   System.out.println(p.name);
}
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
  • 1
    Thank you. Sorry this is a dumb question but i'm new – George Aug 10 '13 at 18:04
  • 1
    @George: If you have any doubts, check out this previous question about it: http://stackoverflow.com/questions/40480/is-java-pass-by-reference?answertab=votes#tab-top – Andrew Martin Aug 10 '13 at 18:11
1

Object reference variables are passed by value in Java (like everything else). In someMethod(), the name of the actual object to which p points is changed. In anotherMethod(), it points to a new Person object with its information changed. Your original reference is unchanged. Outside of these methods, you still have some variable pointing to the original Person object.

It's best to think about these variables as pointers. You create a Person p, and it points to this new object with a name you've decided on. Let's say you create another variable, Person x and set x = p. Now x and p point to the same object. Changing one will reflect itself in both variables. However, if you then do p = new Person(), then p will point to a NEW object, but x will still point to that original object first referenced by p.

Kon
  • 10,702
  • 6
  • 41
  • 58
0

Java does not use references in the C++ sense. so your method

static void anotherMethod(Person p) {
p = new Person();
p.name = "anotherMethod";
System.out.println(p.name);
}
}

does nothing to the p variable, it simply creates new object, calls it (temporarly) p and forgets about it once the method is completed.

p is just a copy of the reference to the actual object, Java always passes arguments as copies.

lejlot
  • 64,777
  • 8
  • 131
  • 164
0

Java passes all objects by copies of the reference, not by reference.

That means that p in anotherMethod is a copy of the reference p that lives in the main method. If you assign the p that lives in anotherMethod is has no impact on what p in your main method references, because it's just a copy.

Xabster
  • 3,710
  • 15
  • 21