Commit fa8e4be
committed
feat: Demonstrate use of
WHAT the code does:
- Defines class Human with private fields:
- int age
- String name
- Provides getters: getAge() and getName().
- Provides two styles of setters:
- Current version: `SetAge(int age, Human obj)` assigns age to a passed Human object reference (`obj1.age = age`).
- Alternative (commented): `SetAge(int a)` uses `this.age = a;`, assigning to the current object.
- setName(String name) sets the current object’s name using `this.name = name;`.
- Main class This:
- Creates Human object obj.
- Calls obj.SetAge(30, obj), passing itself to assign age = 30.
- Calls obj.setName("Reddy").
- Prints “Reddy : 30”.
WHY this matters:
- Demonstrates difference between:
- Using `this` → refers to the current object instance on which the method is invoked.
- Passing an object reference as parameter → modifies whichever object is passed in.
- Shows why **setters usually use `this`**: it avoids the need to pass the object itself.
- Illustrates encapsulation by keeping fields private and exposing getters/setters.
HOW it works:
1. Human obj = new Human().
2. obj.SetAge(30, obj) assigns 30 to the obj’s age field through parameter reference.
3. obj.setName("Reddy") assigns "Reddy" to obj’s name.
4. Printing → "Reddy : 30".
Tips and gotchas:
- Standard setter pattern in Java is `this.field = parameter;`.
- Passing the object into its own setter is redundant and unconventional.
- If method parameter has same name as field, `this.field = field;` disambiguates.
- Using `this` improves clarity and avoids bugs where local variables shadow instance variables.
Use-cases / analogies:
- Think of `this` like saying “me, myself” — when you want to update your own details, you don’t hand yourself in as a parameter.
- Passing object explicitly is like writing your name on a note and giving it back to yourself, then using that note to update your records — possible, but unnecessary.
Short key: java this-keyword setter encapsulation parameter-vs-instance disambiguation.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>this vs explicit object reference in setters1 parent 874375d commit fa8e4be
1 file changed
+11
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
4 | 2 | | |
5 | 3 | | |
6 | 4 | | |
7 | | - | |
8 | | - | |
| 5 | + | |
9 | 6 | | |
10 | 7 | | |
11 | | - | |
12 | | - | |
| 8 | + | |
| 9 | + | |
13 | 10 | | |
14 | | - | |
15 | | - | |
| 11 | + | |
| 12 | + | |
16 | 13 | | |
17 | 14 | | |
18 | 15 | | |
| |||
21 | 18 | | |
22 | 19 | | |
23 | 20 | | |
24 | | - | |
25 | | - | |
26 | | - | |
| 21 | + | |
27 | 22 | | |
28 | 23 | | |
29 | | - | |
30 | | - | |
| 24 | + | |
| 25 | + | |
31 | 26 | | |
32 | 27 | | |
33 | 28 | | |
| |||
39 | 34 | | |
40 | 35 | | |
41 | 36 | | |
42 | | - | |
43 | | - | |
| 37 | + | |
44 | 38 | | |
45 | 39 | | |
46 | 40 | | |
| |||
49 | 43 | | |
50 | 44 | | |
51 | 45 | | |
52 | | - | |
53 | 46 | | |
54 | | - | |
| 47 | + | |
0 commit comments