13

Recently from this question I have learned that the following appears to be legal java:

class Bar {
    void foo(Bar this) {}
}

Now, I have tried to find where in the java standard it says that you are allowed to do this, and looked here but I couldn't find the section.

Can someone quote where it allows this form of method declaration and what the restrictions of declaring an argument named this are?

WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
  • Thats cool. Since I knew I tried to figure that "JLS" background; but my initial lazy attempts failed. And thanks to you, I now know! – GhostCat Sep 16 '16 at 16:33

1 Answers1

13

It is valid with 1.8 or above JDK versions.

And here is the JLS saying that

The receiver parameter is an optional syntactic device for an instance method or an inner class's constructor. For an instance method, the receiver parameter represents the object for which the method is invoked. For an inner class's constructor, the receiver parameter represents the immediately enclosing instance of the newly constructed object. Either way, the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated.

If you read full bullet points you find the below imp notes,

Where a receiver parameter is allowed, its type and name are specified as follows:

  • In an instance method, the type of the receiver parameter must be the class or interface in which the method is declared, and the name of the receiver parameter must be this; otherwise, a compile-time error occurs.

  • In an inner class's constructor, the type of the receiver parameter must be the class or interface which is the immediately enclosing type declaration of the inner class, and the name of the receiver parameter must be Identifier . this where Identifier is the simple name of the class or interface which is the immediately enclosing type declaration of the inner class; otherwise, a compile-time error occurs.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Huh... what was the reason for this change? It feels a bit unnecessary to be able to name a parameter 'this' – Gikkman Sep 16 '16 at 16:19
  • 2
    @Gikkman The last sentence explains why: *the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, **so that the type may be annotated**.* Though I think it really means: so that the (implicit) *parameter* may be annotated. – Andreas Sep 16 '16 at 16:20
  • 2
    I've read your edit like 5 times and I still don't get it. Guess I found something I need to look up further here, thanks :-) – Gikkman Sep 16 '16 at 16:26
  • I would have found it useful if the `this` could have been generic (bounded by the enclosing class type), it seems that isn't possible – WorldSEnder Sep 16 '16 at 16:28
  • 1
    @Gikkman - for example, consider if you're using the "checker" frameworks's [map key checker annotations](http://types.cs.washington.edu/checker-framework/current/checker-framework-manual.html#map-key-checker) to ensure your map references are valid at compile time you could have a method `String lookMeUp(@KeyFor("map") MyClass this) { return map.get(this); }` and it would only allow you to call the method if the object you're calling it on is known to be a valid key for the given map. – Periata Breatta Sep 16 '16 at 17:25