Skip to content

Commit 27b46fa

Browse files
committed
feat: Demonstrate getter-only design with immutable field in Student11 class
WHAT the code does: - Defines `Student11` with a private, final `id`. - Provides a constructor for initialization. - Supplies a **getter** (`getId()`) for read-only access. - No setter provided → making the object’s `id` immutable after creation. WHY this matters: - Enforces immutability (field values cannot be changed once set). - Enhances **data security** by controlling access. - Example of encapsulation best practice in OOP. HOW it works: 1. `private final String id;` ensures the field must be initialized once, and never modified. 2. Constructor sets the `id` when creating a new `Student11` object. 3. Only `getId()` exposes the value externally. 4. Any attempt to call `setId()` (commented in code) results in compilation error. Use-cases: - Immutable objects in finance, IDs, tokens, and keys. - Prevent accidental or malicious modification of sensitive data. Short key: getter-immutable-field. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 05254de commit 27b46fa

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed
Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1-
/*
2-
It seems like you're demonstrating the concept of a read-only property in Java by creating a `GetAndSerAdvance` class (though the class name seems to be a typo—it's referring to a "Student" in the constructor). Here's a breakdown of your example and a few adjustments for clarity:
3-
4-
### Key Concepts in Your Code:
5-
1. **Read-only Property**: The `id` field is marked as `final`, meaning it cannot be modified after being initialized in the constructor. The `getId()` method is the only way to access the `id` value, making it read-only.
6-
7-
2. **No Setter Method**: By not providing a setter method, the value of `id` cannot be changed once it’s set in the constructor. This is a way of ensuring immutability or protecting the integrity of the object's state.
8-
9-
### Updated Example with Corrections:
10-
11-
### Explanation:
12-
- **The `id` field**: It's marked as `private final`, which means it’s initialized once and can't be modified after that.
13-
- **Constructor**: The constructor takes a parameter `id` and assigns it to the field.
14-
- **Getter**: The `getId()` method allows reading the value of `id` but prevents modification.
15-
- **No setter**: There is no setter method like `setId()`, so the `id` is effectively read-only.
16-
17-
### Potential Use Case:
18-
This design pattern is useful for creating immutable objects, where the state of an object doesn't change after it’s created, ensuring its consistency throughout the program. For example, it’s common in scenarios involving identifiers, configuration settings, or other values that should not change once set.
19-
*/
20-
211
class Student11 {
222
private final String id;
23-
243
// Constructor initializes 'id' with the provided value
254
public Student11(String id) {
265
this.id = id;
276
}
28-
297
// Getter method for 'id', providing read-only access
308
public String getId() {
319
return id;
3210
}
3311
}
34-
35-
// Usage
3612
public class GetAndSetAdvance {
3713
public static void main(String[] args) {
3814
Student11 student = new Student11("S123");
3915

40-
System.out.println(student.getId()); // Output: S123
41-
42-
// student.setId("S456"); // Error: No setter method available
16+
System.out.println(student.getId());
17+
// student.setId("S456");
18+
// Error: No setter method available
4319
}
44-
}
20+
}
21+
22+
/*
23+
It seems like you're demonstrating the concept of a read-only property in Java by creating a `GetAndSerAdvance`
24+
class (though the class name seems to be a typo—it's referring to a "Student" in the constructor).
25+
26+
Here's a breakdown of your example and a few adjustments for clarity:
27+
28+
Key Concepts in Your Code:
29+
1. Read-only Property: The `id` field is marked as `final`, meaning it cannot be modified after being initialized in the constructor.
30+
The `getId()` method is the only way to access the `id` value, making it read-only.
31+
32+
2. No Setter Method**: By not providing a setter method, the value of `id` cannot be changed once it’s set in the constructor.
33+
34+
This is a way of ensuring immutability or protecting the integrity of the object's state.
35+
36+
Updated Example with Corrections:
37+
38+
Explanation:
39+
- The `id` field: It's marked as `private final`, which means it’s initialized once and can't be modified after that.
40+
- Constructor: The constructor takes a parameter `id` and assigns it to the field.
41+
- Getter: The `getId()` method allows reading the value of `id` but prevents modification.
42+
- No setter: There is no setter method like `setId()`, so the `id` is effectively read-only.
43+
44+
### Potential Use Case:
45+
This design pattern is useful for creating immutable objects,
46+
where the state of an object doesn't change after it’s created, ensuring its consistency throughout the program.
47+
48+
For example, it’s common in scenarios involving identifiers, configuration settings, or other values that should not
49+
change once set.
50+
*/

0 commit comments

Comments
 (0)