I’m guessing the following two functions compile to the exact same byte-code, but I beg to ask the question. Does qualifying a method call where it is not necessary degrade performance?
For example:
package com.my;
import android.app.Activity;
import android.os.Bundle;
public class Main extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.main); // Fully qualified
setContentView(R.layout.main); // Not fully qualified
}
}
The standard is to use the latter, but does the fully qualified call this.setContentView() have any negative effects?
No, there isn’t any sort of penalty. Both calls will use
invokespecialin the bytecode to invoke the member method.In standard Java, the following code:
yields the following bytecode:
As you can see, both calls are the same.
Now Android converts the
.classfiles into dalvik bytecode using thedxtool, which outputs.dexfiles. Since the.classfiles don’t show any distinction between the two invocations, the corresponding.dexfile will show no difference either:You can see that both calls are made using
invoke-direct. Hence there is no performance penalty here either.