Accessing Objects through References
Employee e1 = new Employee();
Employee e2 = new Employee();
// e1 and e2 refer to two independent Employee objects on the heap
Employee e3 = e1;
// e1 and e3 refer to the *same* Employee object
e3 = e2;
// Now e2 and e3 refer to the same Employee object
e1 = null;
// e1 no longer refers to any object. Additionally, there are no references
// left to the Employee object previously referred to by e1. That "orphaned"
// object is now eligible for garbage collection.
| Note |
The statement Employee e3 = e2; sets e3 to point to the same physical object as e2 . It does not duplicate the object. Changes to e3 are reflected in e2 and vice-versa. |
No comments:
Post a Comment