I recently started making simple 2D games, and came across a dilemma – to use or not to use getter and setter methods for the x and y positions of an object. It would look clearer if i wouldn’t use methods, but access the variables directly instead, and also, many method calls on the object would hurt performance. Also getting and setting the position pretty much remains simple, so there is really no need for encapsulation here? Or should we always stick to the convention of getter and setter methods at all times?
Share
It’s always better to stick to the convention. Plus, the JIT will simplify operations in order to access directly to the field instead of going through all the methods stack.
It’s called method inlining.
Resources :
On the same topic :