[type-annos-observers] Clarifying the receiver parameter

Alex Buckley alex.buckley at oracle.com
Wed Apr 10 17:54:09 PDT 2013


There is some confusion around i) the type and ii) the name of the 
formal parameter which represents the receiver of an inner class's 
constructor or instance method.

The current text is just "For a method, the receivers are named this, 
Outer.this, etc." and "Within an inner class constructor, the receiver 
has a name such as Outer.this". This doesn't help with the legality of 
the following receiver parameters, both of which should be legal:

   package p;
   class Outer {
     class Inner {
       Inner(p.Outer p.Outer.this) {}
     }
   }

   package p;
   class Outer {
     class Middle {
       class Inner {
         Inner(Outer.Middle Outer.Middle.this) {}
       }
     }
   }

It needs to be stated that: "The receiver of a constructor or instance 
method of an inner class C, a direct inner class of O (8.1.3), is 
denoted by a formal parameter which appears first in the formal 
parameter list, and whose type is O, and whose name is a qualified this 
expression (15.8.4) which would, if used in the body of the constructor 
or instance method, refer to the immediately enclosing instance of the 
instance of C."

By saying "whose type is O", we delegate to the normal process of 
resolving a type name. And by saying the name is that of a qualified 
this expression, we get the type resolution of the ClassName in a 
qualified this.

Finally, we should apply the same technique for non-inner ctors/methods:

"The receiver of a constructor or instance method of a top-level class C 
is denoted by a formal parameter which appears first in the formal 
parameter list, and whose type is C, and whose name is a this expression 
(15.8.3) or qualified this expression (15.8.4) which would, if used in 
the body of the constructor or instance method, refer to the instance of C."

which rightly allows:

package p;
class C {
   void m(p.C p.C.this) {}
}

(We need to allow a qualified this expression as the name because such 
an expression in the body can refer to _any_ lexically enclosing 
instance of C _including the zeroth lexically enclosing instance of C_ 
which is C itself.)

Alex


More information about the type-annotations-spec-observers mailing list