Is it bad practice to change my getter method like version 2 in my class.
Version 1:
public String getMyValue(){
return this.myValue
}
Version 2:
public String getMyValue(){
if(this.myValue == null || this.myValue.isEmpty()){
this.myValue = "N/A";
}
return this.myValue;
}
Do what ever you like. After all getters and setters are just another public methods. You could use any other names.
But if you use frameworks like
Spring
, you are bound to use those standard names and you should never put your custom codes inside them.I usually define a specific
getter
.Never alter original
getter
:And create an specific
getter
: