2

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?

Tyler
  • 19,113
  • 19
  • 94
  • 151
  • Being more specific should never *degrade* performance, though it may not *help* it. – Dave Newton Oct 25 '11 at 16:40
  • If I were to ask the question again, I would inverse the proposition.. you are right, the degradation would result from *not* qualifying. – Tyler Oct 25 '11 at 18:13

2 Answers2

9

No, there isn't any sort of penalty. Both calls will use invokespecial in the bytecode to invoke the member method.

In standard Java, the following code:

public class TestQualified {

   private void someMethod() {
   }

   public void otherMethod() {
       this.someMethod();
       someMethod();
   }
}

yields the following bytecode:

Compiled from "TestQualified.java"
public class TestQualified extends java.lang.Object{
public TestQualified();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

private void someMethod();
  Code:
   0:   return

public void otherMethod();
  Code:
   0:   aload_0
   1:   invokespecial   #2; //Method someMethod:()V
   4:   aload_0
   5:   invokespecial   #2; //Method someMethod:()V
   8:   return

}

As you can see, both calls are the same.

Now Android converts the .class files into dalvik bytecode using the dx tool, which outputs .dex files. Since the .class files don't show any distinction between the two invocations, the corresponding .dex file will show no difference either:

Processing 'TestQualified.dex'...
Opened 'TestQualified.dex', DEX version '035'
Class #0            -
  Class descriptor  : 'LTestQualified;'
  Access flags      : 0x0001 (PUBLIC)
  Superclass        : 'Ljava/lang/Object;'
  Interfaces        -
  Static fields     -
  Instance fields   -
  Direct methods    -
    #0              : (in LTestQualified;)
      name          : '<init>'
      type          : '()V'
      access        : 0x10001 (PUBLIC CONSTRUCTOR)
      code          -
      registers     : 1
      ins           : 1
      outs          : 1
      insns size    : 4 16-bit code units
0000e4:                                        |[0000e4] TestQualified.<init>:()V
0000f4: 7010 0300 0000                         |0000: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0003
0000fa: 0e00                                   |0003: return-void
      catches       : (none)
      positions     : 
        0x0000 line=1
      locals        : 
        0x0000 - 0x0004 reg=0 this LTestQualified; 

    #1              : (in LTestQualified;)
      name          : 'someMethod'
      type          : '()V'
      access        : 0x0002 (PRIVATE)
      code          -
      registers     : 1
      ins           : 1
      outs          : 0
      insns size    : 1 16-bit code units
0000fc:                                        |[0000fc] TestQualified.someMethod:()V
00010c: 0e00                                   |0000: return-void
      catches       : (none)
      positions     : 
        0x0000 line=4
      locals        : 
        0x0000 - 0x0001 reg=0 this LTestQualified; 

  Virtual methods   -
    #0              : (in LTestQualified;)
      name          : 'otherMethod'
      type          : '()V'
      access        : 0x0001 (PUBLIC)
      code          -
      registers     : 1
      ins           : 1
      outs          : 1
      insns size    : 7 16-bit code units
000110:                                        |[000110] TestQualified.otherMethod:()V
000120: 7010 0200 0000                         |0000: invoke-direct {v0}, LTestQualified;.someMethod:()V // method@0002
000126: 7010 0200 0000                         |0003: invoke-direct {v0}, LTestQualified;.someMethod:()V // method@0002
00012c: 0e00                                   |0006: return-void
      catches       : (none)
      positions     : 
        0x0000 line=7
        0x0003 line=8
        0x0006 line=9
      locals        : 
        0x0000 - 0x0007 reg=0 this LTestQualified; 

  source_file_idx   : 3 (TestQualified.java)

You can see that both calls are made using invoke-direct. Hence there is no performance penalty here either.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
1

No it doesn't. It's all the same for the compiler. Using this reference may be seen as a reading help, like the underscores in the java7 number literals

Community
  • 1
  • 1
kostja
  • 60,521
  • 48
  • 179
  • 224