Alex Rivera | Logout

In Java, what is the difference between this.method() and method()?

Asked 2011-07-01T11:22:32.230
50

Is there any difference between calling this.method() and method() (including performance difference)?

Edit
Report

1 Answer

19

That there is no difference can be seen by calling javap -c ClassName on the commandline. For example:

public class This {
    private void method() {
    }
    public void noThis() {
        method();
    }
    public void useThis() {
        this.method();
    }
}

Produces the following disassembled output:

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

public void noThis();
  Code:
   0:   aload_0
   1:   invokespecial   #2; //Method method:()V
   4:   return

public void useThis();
  Code:
   0:   aload_0
   1:   invokespecial   #2; //Method method:()V
   4:   return

}
answered 2011-07-01T11:35:58.143

Your Answer