Skip to content

Commit fa8e4be

Browse files
committed
feat: Demonstrate use of this vs explicit object reference in setters
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]>
1 parent 874375d commit fa8e4be

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Section16StaticFinal/src/This.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
class Human
2-
{
3-
1+
class Human {
42
private int age;
53
private String name;
64

7-
public int getAge()
8-
{
5+
public int getAge() {
96
return age;
107
}
11-
public void SetAge(int age, Human obj)
12-
{
8+
9+
public void SetAge(int age, Human obj) {
1310
//Human obj1=new Human();
14-
Human obj1=obj;
15-
obj1.age=age;
11+
Human obj1 = obj;
12+
obj1.age = age;
1613
//this.age=age;
1714
}
1815

@@ -21,13 +18,11 @@ public void SetAge(int age, Human obj)
2118
// age=a;
2219
// }
2320

24-
25-
public String getName()
26-
{
21+
public String getName() {
2722
return name;
2823
}
29-
public void setName(String name)
30-
{
24+
25+
public void setName(String name) {
3126
this.name=name;
3227
}
3328

@@ -39,8 +34,7 @@ public void setName(String name)
3934
}
4035

4136
public class This {
42-
public static void main(String[] args) throws ClassNotFoundException
43-
{
37+
public static void main(String[] args) throws ClassNotFoundException {
4438
Human obj=new Human();
4539

4640
obj.SetAge(30,obj);
@@ -49,6 +43,5 @@ public static void main(String[] args) throws ClassNotFoundException
4943

5044
// System.out.println(obj.abc()+" : "+obj.getAge());
5145
System.out.println(obj.getName()+" : "+obj.getAge());
52-
5346
}
54-
}
47+
}

0 commit comments

Comments
 (0)