This is just the behavior of the reference type. In your example, two refers to the same object as one after assignment. However, when re-assigning one new object does not affect two ; this is the behavior you see.
You will see the same behavior with other link objects, for example
StringBuilder one = new StringBuilder("10"); StringBuilder two = one; one = new StringBuilder("20"); // two still references StringBuilder with "10"
In order for the reference class to show behavior when one object changes, it also changes another, the class must be changed, like the OwnInteger class in your code, and the code must change the object, rather than reassigning it. Wrapper classes such as Integer are immutable, so you won't run into this behavior.
source share