For an inner class's constructor, the receiver parameter represents the immediately enclosing instance of the newly constructed object. For an instance method, the receiver parameter represents the object for which the method is invoked. The receiver parameter is an optional syntactic device for an instance method or an inner class's constructor. Which enable us to do enhanced type checking.įor example this code may be a compile time restriction for the callers if they are not calling calc method on an instance of Calculator which has presumably been loaded from the server. Why do we need Explicit Receiver Parameters? For example this is a compile time errorĮrror:(7, 34) java: the receiver type does not match the enclosing class type We cannot restrict the explicit receiver parameters based on subtype of generics. In both above examples we can : calculator.calc() Įxplicit receiver doesn't create new overloaded method. Explicit receiver parameter doesn't make any difference inside the body of the declaring method.Īlso there's no runtime difference in calling a method will this explicit declaration. In both examples this.getSomething() is on the same 'this' instance. 'this' in first code snippet has no special meaning. Semantically above method is same as declaring this one: In Java 8, we can optionally declare an instance method having a parameter of enclosing type with 'this' keyword:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |