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

The only time it matters is if you are using OuterClass.this.method() e.g.

class OuterClass {
    void method() { }

    class InnerClass {
        void method() {
            OuterClass.this.method(); // not the same as method().
        }
    }
 }

There is absolutely no difference between these constructs and the generated bytecode will be exactly the same, hence no performance impact. this is resolved during compilation if not explicitly defined.

The only reason for using explicit this is readability - some people find it easier to read because this suggests that this is an instance method of current object.

Also please note that if method() is static, using this is discouraged and misleading.

private static void method() {
}

private void foo() {
    this.method();    //generates warning in my IDE for a reason
}

And in this case it will also have no effect on performance.


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

}

For methods there is no difference, but it can make a difference with fields. Consider this code:

private String someObject = "some value";

public String someMethod(String someObject) {
     //this.someObject refers to the field while
     //someObject refers to the parameter
}

There is no real difference - At least there is no performance impact. I prefer not writing "this" - The IDE can usually highlight calls to this anyway, and I think it is less readable when every access to methods/fields/... start with "this.". But it really is a matter of personal preference.