Clarifying the receiver parameter

Michael Ernst mernst at cs.washington.edu
Tue Oct 22 10:29:06 PDT 2013


There seems to be a bit of confusion between the result of a constructor 
and the receiver of a constructor.  (I'm focusing for the purpose of this 
discussion on inner class constructors, which are the only ones that have 
receivers.)

Recall that constructor receivers and constructor results are completely 
different concepts.  (But Java's slightly odd naming conventions make them 
easy to confuse!)

  * in a (non-constructor) method, the receiver is named "this"
  * in a (non-constructor) method, there is no special name for result; 
the result is just whatever expression appears in the return statement
  * in a top-level constructor, there is no receiver
  * in an inner class constructor, the receiver is named "Outer.this".  It 
cannot be referred to as "this", because "this" means something else, 
namely the result.
  * in a constructor, the result is named "this", and there is no explicit 
return statement

Writing "this" as the name of the receiver in a constructor declaration 
would be incorrect, and one must write "Outer.this" to disambiguate.  This 
is exactly the same as the case within the constructor body:  to refer to 
the result, one uses "this", but to refer to the receiver, one uses 
"Outer.this".  So, the declaration is consistent with usage within the 
method body.  It would be rather confusing for "this" to mean one thing in 
the declaration and a different thing in the body.


I hope this explains the rationale and helps to answer Srikanth's and 
Markus's questions.

                     -Mike


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