-2

Is this a legit use of getter

Lady lady = new Lady();
lady.getWater() = "hot water";

if we suppose getter returns

Class Lady {
public String getWater() {
     this.water;
}}

?

ZHU
  • 129

1 Answers1

3

Short answer: NO

Long answer: You are misusing the Getter. You have Setters & Getters to encapsulate data, i.e. prevent direct access to the member variables. In your Setter, you might e.g. check that you actually set some kind of water (hot/cold/soapy). If you abuse the Getter to Set data you circumvent that. Also, it runs contrary to the expected use of Getters, so anyone else working on your code will be in for unpleasant surprises.

To conclude: This is all kinds of bad (I'm not sure it would even work in Java) don't do it!

Li Chen
  • 123
CharonX
  • 1,719