Is it better use getter methods or to access private fields directly when overriding toString? [duplicate]

StackOverflow https://stackoverflow.com/questions/2073159

  •  20-09-2019
  •  | 
  •  

Question

I've seen both approaches used but have never heard that one way is preferred over the other for any particular reason.

public String toString() {
  return this.field1 + " " + this.field2;
}

versus

public String toString() {
  return getField1() + " " + getField2();
}

I used String concatenation in my example to keep the code brief.

Was it helpful?

Solution

Use getters, if you have them!

Maybe, one day you change the code so that a getter will not only return the fields value but do something more or create the result in a different way. Then you'll be more then happy that you used getters consistently.

But as usual, there are excemptions from my advice - what do you expect from the toString() method if you allow overriding of the getter methods, do you want it use the classes fields or the result of the - maybe override - getter method.

So, as usual, it depends, but I'd use getters unless I have a good reason to access the fields directly.

OTHER TIPS

Using accessor methods might be preferred if they provide any additional logic on top of just returning the field's value (like uppercasing a string, or something like that). If your class is designed to be extended, I would tend towards accessors because they may be overridden.

Using getters in toString is an overkill. And you should not be using toString for non debug purposes.

I would prefer to the approach to access the private variables directly. IMHO the gettter methods increase the clutter.

Do you have any reason to use getter methods? Do they do anything interesting?

...Or are you just using them to expose internal data without actually making the fields themselves public (poor man's encapsulation)?

If the former, then yes by all means use them here as well! If the latter, then it doesn't matter - you might want to use them anyway, just in case you ever give them a real purpose, but it won't make one bit of difference otherwise.

using String.format("%s %s", this.getField1(), this.getField2()); would be the best way to do this, becuase the .getXXX() methods might have logic in them that you want like converting NULL references into empty strings or some default value that you would not get by just blindly accessing the private instances. It is the safest way to do it, just in case someone comes along and adds logic to a .getXXX() method and they don't think to update the .toString() implemenation.

If your class has getters and setters for fields, you should be consistent throughout the implementation of that class in whether you use getters and setters or access the fields directly. It doesn't generally make sense to mix levels of abstraction. Otherwise, how can you rationalize using them in some places but not in others? In addition, if there is ever a change in how the getters are implemented, you could end up with subtle bags. toString() is part of the implementation, so it should be consistent as well.

In terms of performance, it shouldn't matter - your trivial getters and setters should be final methods anyway, so they should be inlined anyway. IF they're not final, you should ask yourself why and probably avoid accessing the fields directly unless this is really what you mean to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top