Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setter and Getter functions. In Android. Performance overhead?

When I was learning Java standard edition, getter and setter functions were often used to hide variables and to reduce direct access to them. I have been told by several sources that in Android you should not use these functions and only modify the variables directly. The reason is that there is a performance loss due to overhead when using getters and setters is used in Android. Resulting in more memory use and slowing down the system.

Is there any truth to this? and if there is, why the performance loss for using getter and setter?

like image 201
Kevik Avatar asked Dec 19 '12 02:12

Kevik


1 Answers

As of the documentation found here using a getter and setter is a bad idea in android. As it says,

this is a bad idea on Android. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.

Find more about performance here.

like image 171
Vinay Avatar answered Sep 21 '22 10:09

Vinay