Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Performance - 'Avoid Internal Getters/Setters'
    text
    copied!<p>Just read this on the dev site:</p> <p><a href="http://developer.android.com/guide/practices/design/performance.html#internal_get_set" rel="noreferrer">Avoid Internal Getters/Setters</a></p> <blockquote> <p>In native languages like C++ it's common practice to use getters (e.g. i = getCount()) instead of accessing the field directly (i = mCount). This is an excellent habit for C++, because the compiler can usually inline the access, and if you need to restrict or debug field access you can add the code at any time.</p> <p>On Android, this is a bad idea. 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.</p> <p>Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter. This is true in Froyo, but will improve in the future when the JIT inlines getter methods.</p> </blockquote> <p>So is it saying you would use field access within the class:</p> <pre><code> public class MyObject { public Object innerObject; // This would be private if I was using a getter public void doSomeStuff(){ if(innerObject){ // Within class access like this // .... } } public Object getInnerObject(){ // This would be removed if I was using field access return innerObject; } } </code></pre> <p><strong>But what about access from another object</strong>?:</p> <pre><code> public class SecondObject { public void doSecondSomething(){ MyObject ob = new MyObject(); Object inner; //This is my question basically (from an Android performance perspective) inner = ob.getInnerObject(); // OR inner = b.innerObject } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload