Skip to content

Commit dc61946

Browse files
committed
docs: Explain use of this keyword for disambiguating constructor parameters
WHAT the update covers: Adds detailed explanation and examples showing why `this.itemNo = itemNo;` is required in constructors when parameter names shadow instance variables. WHY this matters: Clarifies the difference between local parameters and instance variables in Java. Without `this`, assignments like `itemNo = itemNo;` only reassign the parameter to itself, leaving the instance variable uninitialized (defaulting to null or 0). Using `this.itemNo` explicitly refers to the object’s field, ensuring correct initialization. HOW it works: Incorrect case: - public Product(String itemNo) { itemNo = itemNo; } - Creates a Product but leaves the field itemNo null. Correct case: - public Product(String itemNo) { this.itemNo = itemNo; } - Properly assigns the parameter value to the instance variable. Demonstrated with examples showing null output without `this` vs correct "A25" output with `this`. Tips and gotchas: `this` is only required when parameter names shadow instance variables; otherwise, it can be omitted. Using `this` improves readability and avoids subtle bugs, especially in constructors and setters. Overusing identical parameter and field names can cause confusion; some developers adopt naming conventions like prefixing fields (e.g., itemNo vs itemNoParam). Use-cases: Educational note for beginners learning constructors and `this` keyword. Prevents common mistakes in object initialization. Forms the basis for understanding scope resolution in OOP. Short key: docs-this-keyword constructor-parameter-shadowing. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent ae1cc9e commit dc61946

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Section11ObjectOrientedProgramming/src/Section11ProductAndCostomer.txt renamed to Section11ObjectOrientedProgramming/src/Explain use of `this` keyword.txt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Why Do We Need this.itemNo = itemNo; in the Constructor?
22

3-
If you don’t use `this.itemNo = itemNo;` in the constructor, the instance variable (`itemNo`) won’t be assigned the value passed as a parameter.
3+
If you don’t use `this.itemNo = itemNo;` in the constructor, the instance variable (`itemNo`) won’t be assigned the
4+
value passed as a parameter.
45

56
What Happens If We Don't Use `this`?
67

@@ -22,6 +23,7 @@ public Product(String itemNo) {
2223
}
2324

2425
```
26+
2527
- `this.itemNo` refers to the instance variable of the class.
2628
- `itemNo` (without `this`) refers to the local parameter passed to the constructor.
2729
- `this.itemNo = itemNo;` assigns the constructor parameter to the instance variable.
@@ -30,16 +32,13 @@ Example: Without `this` (Incorrect Code)
3032

3133
class Product {
3234
private String itemNo;
33-
3435
public Product(String itemNo) {
3536
itemNo = itemNo; // ❌ Wrong! This does nothing
3637
}
37-
3838
public String getItemNo() {
3939
return itemNo;
4040
}
4141
}
42-
4342
public class Main {
4443
public static void main(String[] args) {
4544
Product p = new Product("A25");
@@ -72,7 +71,6 @@ public class Main {
7271
```
7372
🟢 Now, `getItemNo()` returns `"A25"` as expected.
7473

75-
Conclusion
76-
74+
Conclusion:
7775
When the parameter name is the same as the instance variable name, use `this` to refer to the instance variable.
78-
Without `this`, the assignment does nothing, leading to incorrect behavior (like getting `null`).
76+
Without `this`, the assignment does nothing, leading to incorrect behavior (like getting `null`).

0 commit comments

Comments
 (0)