-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectReference.java
More file actions
27 lines (20 loc) · 987 Bytes
/
Copy pathObjectReference.java
File metadata and controls
27 lines (20 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ObjectReference {
int value;
ObjectReference(int value) {
this.value = value;
}
public static void main(String[] args) {
ObjectReference obj1 = new ObjectReference(10);
ObjectReference obj2 = obj1; // obj2 references the same object as obj1
System.out.println("obj1 value: " + obj1.value); // prints 10
System.out.println("obj2 value: " + obj2.value); // prints 10
obj2.value = 20; // modifying obj2 affects obj1
System.out.println("After modification:");
System.out.println("obj1 value: " + obj1.value); // prints 20
System.out.println("obj2 value: " + obj2.value); // prints 20
obj2 = new ObjectReference(30); // obj2 now references a new object
System.out.println("After reassignment:");
System.out.println("obj1 value: " + obj1.value); // still prints 20
System.out.println("obj2 value: " + obj2.value); // prints 30
}
}