Bifalgorithm post · Computer science one · Accessor & Mutator Methods
How do accessor and mutator methods use this to access instance variables?
How do accessor and mutator methods use this to access instance variables? (Computer science one · Accessor & Mutator Methods) — "Since we have made all the me…
Ideas in this flowchart
- Java private member variables and getter/setter methods
- Private member variables restrict code outside the class from accessing or modifying their values.
- Accessor and mutator methods allow controlled external access to an instance's variables.
- A getter returns the value of an instance variable.
- A setter accepts a value and assigns it to an instance variable.
- Getter and setter names commonly use the get or set prefix with lower camel casing.
- The original setter has no effect because it assigns the parameter to itself.
- Java scoping rules give the method parameter precedence when the parameter and member variable have the same name.
- The this keyword refers to the current instance inside the class.
- Using this.firstName distinguishes the member variable from the firstName parameter.
- The corrected setter uses this.firstName = firstName, and the getter may use this.firstName as well.
- The faulty assignment is equivalent to writing a = a.