4

This is a knowledge/curiosity question only.

After several years in Java, this has only just struck me.

class Foo {

   class Bar{

      Foo.this.doSomething();

   }

}

When I look at Foo.this, I would assume that it's a static reference which obviously is not the case.

I know this is part of the Java spec, but exactly what is going on when you use <Class>.this?

Is it one of those "it just is" things?

stillanoob
  • 1,279
  • 2
  • 16
  • 29
Simon
  • 14,407
  • 8
  • 46
  • 61
  • @noob. Please don't edit out the Android tag. I only code using Java because I create Android apps. This question has broad relevance to Android devs also. – Simon Apr 13 '13 at 07:12
  • 1
    Firstly, Bhesh Gurung is the one who removed the Android tag, not noob. The fact that you happen to use android is irrelevant to the tagging. I could just as easily tag this question as Windows if I happen to do my Java programming to create Windows apps. Your question is not making use of any android-related functionality, so it doesn't warrent an android tag. I've refrained from reverting your edit, but solely due to fear of a pointless edit war. – Brian Apr 13 '13 at 07:44
  • @Brian. Well, it's all a little silly anyway, but if most Android devs also subscribed to the Java tag, I might agree with you. Since they don't, and Android devs are a very significant group in the set of all Java devs, I respectfully disagree. Thanks for leaving the edit. – Simon Apr 13 '13 at 08:12
  • @Brian. Sorry, BTW, it was stupid of me to not check the edit history and apologies to noob. – Simon Apr 13 '13 at 08:20
  • @Simon: I suggest removing the *android* tag. As it stands, it creates the incorrect impression that this Java question is specific to Android, and thus it gets filtered out when looking for general Java questions while excluding Android-specific questions. Android developers who want to learn about Java in general are free to search for the *java* tag. – O. R. Mapper Jul 02 '14 at 13:20
  • This is definately not Android specific, so that Android tag must be removed. – Dorian Gray Sep 26 '18 at 18:29

1 Answers1

9

I know this is part of the Java spec, but exactly what is going on when you use .this?

It just refers to a "hidden" field within Bar. It's easiest to see this by decompiling. You'll see that there's a Bar constructor taking a reference to an instance of Foo. That reference is stored in a field, and then when you use Foo.this, it just accesses that field. So assuming you'd put your Foo.this.doSomething() into a someMethod call, your code is similar to:

class Foo {

   static class Bar {
      private final Foo $foo;

      Bar(Foo foo) {
          this.$foo = foo;
      }    

      public void someMethod() {
          $foo.doSomething();
      }
   }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks Jon, I never knew that. It's all so clear when laid out like this ;) – Simon Apr 13 '13 at 07:04
  • @JonSkeet Nice answer but if this is what it looks like after being decompiled, how does it work when the inner class has a parameterized constructor? – Mark W Sep 11 '14 at 20:06
  • @MarkW: Why don't you try it to find out? :) (It'll just add an extra parameter to the list that you've written...) – Jon Skeet Sep 11 '14 at 21:15