-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImmutableObjectExample.java
More file actions
171 lines (140 loc) · 4.68 KB
/
ImmutableObjectExample.java
File metadata and controls
171 lines (140 loc) · 4.68 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Day 10 - Encapsulation: Immutable Objects
*
* Concept:
* Immutable objects are objects whose state cannot be changed after creation.
* Once created, their properties cannot be modified.
* Properties are marked as 'final' and there are no setters.
*
* This example demonstrates:
* - Creating immutable objects with final properties
* - No setters (only getters)
* - Benefits of immutability
* - Thread safety of immutable objects
*
* Real-life analogy:
* Like a photograph: Once taken, the photo cannot be changed.
* You can view it (getter) but not modify it (no setter).
*/
public class ImmutableObjectExample {
/**
* Immutable Point class
* Once created, coordinates cannot be changed
*/
static class ImmutablePoint {
// final: Value set once, cannot be modified
private final int x;
private final int y;
/**
* Constructor - only way to set values
*/
ImmutablePoint(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Getter for x (read-only)
*/
int getX() {
return x;
}
/**
* Getter for y (read-only)
*/
int getY() {
return y;
}
/**
* NO SETTERS! State cannot be changed
*/
void display() {
System.out.println("Point(" + x + ", " + y + ")");
}
/**
* Create new point instead of modifying
*/
ImmutablePoint move(int dx, int dy) {
return new ImmutablePoint(x + dx, y + dy);
}
@Override
public String toString() {
return "ImmutablePoint(" + x + ", " + y + ")";
}
}
/**
* Immutable Student class
*/
static class ImmutableStudent {
private final String name;
private final int age;
private final double gpa;
/**
* Constructor
*/
ImmutableStudent(String name, int age, double gpa) {
this.name = name;
this.age = age;
this.gpa = gpa;
}
/**
* Getters only
*/
String getName() { return name; }
int getAge() { return age; }
double getGPA() { return gpa; }
/**
* NO SETTERS!
*/
/**
* To change state, create new object
*/
ImmutableStudent withName(String newName) {
return new ImmutableStudent(newName, age, gpa);
}
ImmutableStudent withAge(int newAge) {
return new ImmutableStudent(name, newAge, gpa);
}
ImmutableStudent withGPA(double newGPA) {
return new ImmutableStudent(name, age, newGPA);
}
@Override
public String toString() {
return name + " (Age: " + age + ", GPA: " + gpa + ")";
}
}
/**
* Main method - entry point
*/
public static void main(String[] args) {
System.out.println("--- Creating Immutable Point ---\n");
// Creating immutable point
ImmutablePoint p1 = new ImmutablePoint(5, 10);
System.out.println("Original point: ");
p1.display();
System.out.println("\n--- Attempting to modify (will fail in compiler) ---");
// p1.x = 20; // COMPILE ERROR! final field cannot be modified
// p1.setX(20); // ERROR! No setter exists
System.out.println("Cannot modify p1 directly!");
System.out.println("\n--- Creating new point instead ---");
ImmutablePoint p2 = p1.move(3, 4);
System.out.println("Original point (unchanged): ");
p1.display();
System.out.println("New point (after move): ");
p2.display();
System.out.println("\n--- Immutable Student Example ---\n");
ImmutableStudent s1 = new ImmutableStudent("Tejas", 20, 3.8);
System.out.println("Original student: " + s1);
// Create new student with different name
ImmutableStudent s2 = s1.withName("Raj");
System.out.println("Original (unchanged): " + s1);
System.out.println("New student: " + s2);
System.out.println("\n--- Benefits of Immutability ---");
System.out.println("1. Thread-safe: No synchronization needed");
System.out.println("2. Predictable: State never changes unexpectedly");
System.out.println("3. Safe map keys: Can use as HashMap keys");
System.out.println("4. Cached safely: Can be cached without issues");
System.out.println("\n--- Immutability Pattern ---");
System.out.println("Instead of: student.setName('New')");
System.out.println("Use: student = student.withName('New')");
}
}