I think using getter methods is better for convenience. Consider a template of a null object , the way to achieve which is as follows:
public String getName(){ if (this.name == null){ this.name = ""; } return this.name; }
This should save you from checking a large number of zeros before working with a variable.
public boolean isCorrect(){ if(this.name != null && this.name.isEmpty()){ //The null check up is boilerplate code return false; }else{ return true; } }
I would rather write this:
public boolean isCorrect(){ if(this.getName().isEmpty()){ //The null check up is boilerplate code return false; }else{ return true; } }
Of course, it depends if you accept this template.
Also consider that you have
double width; double height; public double getWidth(){ return this.width; }
but at some point you decided to change it for the class, but you still have methods to prevent your program from breaking.
Dimension dimension; public double getWidth(){ return this.getDimension().getWidth(); }
Finally (as MadProgrammer commented), when you use inheritance, methods can be overridden to better represent the intended object.
source share