Java Constructor Parameter Shadowing: Understanding the Issue

Today I revisited an important Core Java concept :- Variable Shadowing. Shadowing happens when a constructor parameter has the same name as a class member variable. Example of the problem: private String name; public Employee(String name) { name = name; // Shadow problem } Here, both sides refer to the constructor parameter. The instance variable never gets assigned. Result: The object prints default values like null or 0.0. Correct way using this: public Employee(String name) { this.name = name; // Proper assignment } this refers to the current object’s instance variable. Key takeaway: Without this, constructor parameters can hide instance variables. With this, we clearly differentiate between object state and local scope. A small keyword, but critical for proper object initialization. Strong OOP fundamentals prevent subtle bugs in real systems. #Java #CoreJava #OOP #Encapsulation #SoftwareEngineering #BackendDevelopment

  • text

To view or add a comment, sign in

Explore content categories