Article View: borland.public.jbuilder.java.language
Article #8482Re: extending interfaces and final variables in inner classes
From: John McGrath TeamB
Date: Sat, 24 Mar 2001 23:10
Date: Sat, 24 Mar 2001 23:10
88 lines
3867 bytes
3867 bytes
On Sat, 24 Mar 2001 11:47:05 +0200, Yonatan Taub wrote:
> How about my other question about variables referred from
> inner classes?
Sorry - I missed the other question.
> > When referring to outer objects or variables within an
> > inner class they have to be declared final otherwise you
> > get a compilation error.
> >
> > What's the reason for that?
I have no specific knowledge of Java designers' reasoning on this
point, but I think it is a matter of readability of the code rather
than a technical reason that makes this absolutely necessary.
When an inner class makes reference to variables in an outer scope, the
compiler implements it by passing the values to a hidden constructor,
and the constructor stores them in synthetic member variables. Since
the variables must be final, it is very clear which value is passed in
to the inner class. If that were not required, it would be less clear.
Take the following (invalid) code as an example:
class InnerDemo {
void method() {
int i = 1;
class Inner() {
public void innerMethod() {
System.out.println( i );
}
}
++ i;
Inner inner = new Inner();
inner.innerMethod();
}
}
This would be compiled into the equivalent of this:
class InnerDemo {
void method() {
int i = 1;
++ i;
InnerDemo$1$ inner = new InnerDemo$1$(i);
inner.innerMethod();
}
}
class InnerDemo$1$Inner() {
private int val$i;
public InnerDemo$1$Inner( int i ) {
val$i = i;
}
public void innerMethod() {
System.out.println( val$i );
}
}
The value of "i" is passed to the inner class constructor and stored in
the synthetic member variable "val$i". This happens at the time that
the inner class instance is constructed, although it is very easy to
think that the inner class gets the values from the enclosing scope at
the point at which the inner class is declared.
So in the first example, it would be very easy to think that the value
printed out would be 1, although the second code snippet makes it clear
that the value would be 2.
By only allowing references to final variables in the outer scope, it
eliminates the possibility of confusion. Since the variable's value
never changes, it is clear which value the inner class code gets.
Anyway, that is my theory as to the reason. If anyone has other ideas
or any specific knowledge on why this was done, please post a message.
Of course, Gosling and company could clear this all up by writing "The
Design and Evolution of Java", but as of yet they have not.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<MPG.1526e9b5b360f098a0a5@forums.borland.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!193.174.75.178!news-fra1.dfn.de!news-koe1.dfn.de!news-was.dfn.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews!209.92.40.60References:
<3abbda8f_2@dnews> <MPG.1525f34346561ad298a09f@forums.borland.com> <3abc6d16_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,634447406d21cfca X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-24 15:12:22 PST From: John McGrath [TeamB] <ng-b@JPMcGrath.net> Newsgroups: borland.public.jbuilder.java.language Subject: Re: extending interfaces and final variables in inner classes Date: Sat, 24 Mar 2001 18:10:09 -0500 Message-ID: <MPG.1526e9b5b360f098a0a5@forums.borland.com> References: <3abbda8f_2@dnews> <MPG.1525f34346561ad298a09f@forums.borland.com> <3abc6d16_1@dnews> X-Newsreader: MicroPlanet Gravity v2.30 NNTP-Posting-Host: 209.92.40.60 X-Trace: 24 Mar 2001 15:05:44 -0800, 209.92.40.60 Lines: 88 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!193.174.75.178!news-fra1.dfn.de!news-koe1.dfn.de!news-was.dfn.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews!209.92.40.60