On May 1, 11:10 pm, Lew <l...@lewscanon.com> wrote:
> QQ wrote:
> > Hi all
>
> > Just a quick question.
>
> > Why method-local inner class can access final variable defined in
> > method only?
>
> Because if the variable were not final, the lifetime, scope and
> value-consistency rules would be impossible.
>
> > I know the reason why it can not access instance variable in method.
>
> I believe that is incorrect but don't have time to check it right now.
> Reading the JLS,
> <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3>
> seems to say that local classes can, indeed, access instance variables of the
> lexically enclosing class:
>
>
>
> >> When an inner class refers to an instance variable that is a member of a
> >> lexically enclosing class, the variable of the corresponding lexically
> >> enclosing instance is used.
> and
> >> Inner classes include local (§14.3), anonymous (§15.9.5) and non-static member
> >> classes (§8.5). Here are some examples:
>
> I'll work up an SSCCE later - or better yet, QQ, why don't you do so?
>
> > Just can not figure out why??
>
> One question mark at a time, please.
>
> --
> Lew
Hi Lew
Thanks heaps for your reply, one item you mentioned in your reply is:
inner class can access instance variable defined in outer class, that
is OK, what I was talking about is method-local inner class can only
access method level final variable NOT instance variable.
One thing not quite clear is that, does method level final variable
live longer than method? I google it, just can not find any article
about it.
Thanks again.
Steven
|