So, I recently noticed something on some code I was writing. I could get a variable for a different class/object using dot notation to get the variable: object.someVarable or I could do it the way I was taught and use an accessor method: object.getSomeVariable() to return the variable. So, my question is: which is the preferred way to do this?


This boils down to the coding standard in use. Typically what I've seen in Java is that all member variables are declared private and mutators/accessors (setters/getters) are provided on an as-needed basis.

If the code in question is developed by a third party, and allows both object.someVariable and object.getSomeVariable() then my gut instinct is that they simply didn't bother checking the accessibility of the variable. If this is the case I would still use object.getSomeVariable() because they could realize their mistake in the future and make the variable private. If you use the getter, then you should always be able to do so unless they remove the variable outright.

If the code in question is part of your own code base, I would suggest making the someVariable private and providing the getter. By providing the getter (and setters), you're making a deliberate decision to give users of your code that specific interface. And, if you write code well enough, your users will appreciate those kinds of subtle hints.


Whether or not you should access a field directly by the dot operator or through a getter method depends on what you are doing.

In general, the code that you write should not expose it's interior fields to outside callers without a very good reason. "Because I want to type less" doesn't qualify. So, most of the time you will use a getter method when accessing fields of other objects passed into or created by your class and you should provide getter methods to fields that you want to expose.

The caveat to that is when you are writing code for that is only for the inside of your class. Overriding the Equals(object o) method for example. When overriding Equals for a non-trivial class it is often necessary to compare individual fields in the class. In that case you would generally use dot notation to access the fields of the passed in object (assuming it is of the appropriate type). There are times when you need a helper class which you can define in your class file. Generally you would access the helper object's fields directly unless some logic (which you control, because it's your class) dictates otherwise. The reason it's okay to use the dot operator for these interior uses is because they don't expose anything to outside callers. If you found that you needed the helper class for something else as well and moved it outside the class that was using it, then you would want to use getters.

There are also times when you have to use outside libraries and that code may not have getters and you will be forced to access fields directly.

To reiterate: In general prefer getters.

  • One major value of using setters/getters is that they provide a "single point of entry" to that property. This simple layer of abstraction allows you to, for instance, replace that object property with a call to some other data source within the getter without having to modify every other reference to it within the codebase. – Aaron Cicali May 12 '16 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.