It depends on your use, but in general, I would prefer to use a getter, as this is a βnormalβ way, and in most cases it will do what the class developer expects.
In principle, if the class developer has made the field private, he can freely do what he likes, for example, delete it later, if it can be calculated differently. Then access to the field will break, I hope, right away, if you are not lucky after 3 months, when no one else remembers.
Please note that there are libraries there, such as apache commons BeanUtils (I believe there is one from Spring) that does this for you and offers a more reasonable interface, such as a hash map.
source share