6

Using a getter/setter in the internal code of a class instead of accessing the instance variable directly, is it good or bad practice? At least for setters one could add additional code validating the value, but for the getters it's just overhead? How intelligent is the Java compiler, if my getters/setters just set/get the value directly, will Java optimize my code and replace the getters/setters by direct access to the instance variables, so there's no method calling overhead?

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

3 Answers3

7

There is no need to do that inside a class, unless want to perform additional operations in those getters / setters. The access to the class members can be direct to the class internally, as:

  • The reason for hiding is mainly to hide the implementation (and there's no need to hide the implementation from the class itself)
  • The getters and setters access the members directly, so to call them just to avoid access the members directly is somewhat, umm... redundant.

Regarding performance - I honestly think that in most cases, you shouldn't think about it, you should decide whether to call a method or access directly in terms of readability, scalability and maintenance, not in terms of whether it will take another nano second or not. This approach pays off in the long run. There are places for optimizations, and you should be efficient, but keeping your code clear and maintainable is much more important once the code base is over 20 lines.

MByD
  • 135,866
  • 28
  • 264
  • 277
7

It is more common to access the field directly. The value of a setFieldName method is more obvious for programmers using your code in other classes. With the implementation details hidden, they might not realize what ranges of values are acceptable, so keeping fields private and forcing other developers to go through a setter makes sense. But inside your own class, the case for using a setter is much weaker. If you look at the source for the java API you'll find that getter / setter methods are generally not used within a class.

Thorn
  • 4,015
  • 4
  • 23
  • 42
  • 1
    There's no wrong answer on this topic, but I liked the java api example, that's why I selected this answer. Thanks to this one and also the others! – stefan.at.kotlin May 15 '12 at 21:47
1

Accessing directly is a good thing. However, no one can say getter/setter access is bad, inside the same class. If you are developing a Java bean, you will definitely understand what I am saying. Think you are trying to get the user input of a JTextField as a String. In this case, the getter methods will allow you to do lot of things including String truncating, trim, uppercase, lowercase, etc. If you are trying to do this all just by accessing the direct variable (eg: String s = textField.getText()), you will find it quite difficult to do it. So, what I think is, the good or bad depends on the situation and what you are developing

PeakGen
  • 21,894
  • 86
  • 261
  • 463