-2

If you reference an instantiated object in different classes, do changes get updated to the actual object or just the reference ..

public class First {
     List<String> numbers;
     public First(){
         this.numbers.add("one");
         this.numbers.add("two");
     }
}

public class Second{
     First InstanceOfFirst;
     public Second(First F){
           this.InstanceOfFirst = F;
     }
     public void printList(){
          for(String s : this.InstanceOfFirst.numbers){
               System.out.print(i+", ");
          }
     }
}

public class Third {
     First Reference;
     public Third(First F){
         this.Reference = F;
     }
     public void Update(){
         this.Reference.numbers.add("three");
     }
}

So let's say my main looks likes this:

public static main(String args[]){
    First F = new Frist();
    Second sec = new Second(F);
    Third  th = new Third(F);
    th.Update();
    sec.printList();
}

Would I get one, two or one, two, three as a result.

I guess what I'm trying to ask is: Deos Java make different copies of objects when referenced like this or do they point to the same object??

Please excuse me if my question seems vague ..

Aous1000
  • 2,052
  • 3
  • 16
  • 16
  • 1
    They point to the same object. Have you tried to run this? – nanofarad May 25 '14 at 00:03
  • For this reason, I prefer the term [*Call by \[Object\] Sharing*](http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) .. anyway, this has all [been asked before](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – user2864740 May 25 '14 at 00:07

3 Answers3

2

Java always uses Pass by value whether its a object reference or primitive or literals.

In your case you are passing the reference of First class object and now you are assigning it to someone else that will also point to the same object in the memory(heap) as shown in below snapshot and by reference it can update the state of the object inside the called method as well.

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76
2

The reference points to the object on the heap.

If the object is mutable, and you change its state, then every reference to that object sees the change.

Java doesn't clone or copy anything for you. If you truly want your object to have its own copy, you have to create it.

It's only a problem with mutable classes. Immutable ones, like String, don't suffer from this.

public class Person {

    private String name;
    private Date birthDate;

    public Person(String name, Date birthDate) {
        this.name = name; // no need to clone; String is immutable
        this.birthDate = new Date(birthDate.getTime()); // need to clone, since Date is mutable.
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

You have a reference pointing to the same object, so it would result with one, two, three. By passing your instances this way, you can implement a basic singleton pattern (by having a main class hold those instances).

Java is pass-by-value, but with Objects it passes the value of the reference of that object.

Rogue
  • 11,105
  • 5
  • 45
  • 71