Re: Class.this.. ?
Re: Class.this.. ?
- Subject: Re: Class.this.. ?
- From: Florijan Stamenkovic <email@hidden>
- Date: Tue, 28 Oct 2008 13:18:40 -0400
Hi all,
Going a bit off WO topic here, but maybe it will be interesting to
someone...
I've for the first time today found myself wishing to obtain a
reference to the instance of a nesting class, while having a
reference to an instance of it's inner class, but outside of the
scope of the inner class itself... So, if I was in the scope of the
inner class, I would simply do this:
NestingClass.this
and voila. However, since I am in a method in which I have an
arbitrary instance of the inner class, let's call it "ref", I tried
doing this:
NestingClass.ref
This is a no-go. Now, as far as I understand the concept of class
nesting as it is implemented in Java, this should work. After all, it
is simple to make a method in the inner class that will expose the
instance of the nesting class by returning NestingClass.this. So, I
don't think this is a deliberate attempt to enforce some
encapsulation concept, but an overlook, or something that was seen as
an unnecessary syntactical complication. From my experience, either
of those is in a way understandable, in some 4 years of working with
Java I have only today for the first time wished to do this. Peculiar.
I will google this a bit, and depending on what I find might submit a
bug report to Sun. Any thoughts on this are welcome....
F
On Sep 20, 2008, at 17:46, Florijan Stamenkovic wrote:
On Sep 20, 2008, at 16:36, Ash Mishra wrote:
I understand what it's doing (i.e. accessing the woDisplayGroup
and setting a value in it) but I've not seen the MyListPage.this.
reference before, where the class name is followed by 'this' and
than a variable of the class.
Is this some special Java reference?
If I understand you well, you are asking about the syntactical
implication of this construction:
"ClassName.this"
This type of referencing is used in inner classes to obtain the
instance of the nesting class. In the example you used, it will
ensure that you get a reference to the MyListPage instance, and not
to the instance of the anonymous class that extends
NextPageDelegate (declared in line 9).
While in the piece of code you refer to it is not necessary to do
this, since referencing "myDisplayGroup" can be done directly, it
would be necessary if the anonymous NextPageDelegate subclass also
had a "myDisplayGroup" variable. In that sense I wouldn't say it's
wrong, but just over-cautious.
F
try googling "Java inner class tutorial"
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40mac.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden