Why method local inner class can use final variable rather than...

Q

QQ

Hi all

Just a quick question.

Why method-local inner class can access final variable defined in
method only?

I know the reason why it can not access instance variable in method.

Just can not figure out why??

any reply would be appreciated.

Steven
 
Q

QQ

Because if the variable were not final, the lifetime, scope and
value-consistency rules would be impossible.


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:




I'll work up an SSCCE later - or better yet, QQ, why don't you do so?


One question mark at a time, please.

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
 
P

Patricia Shanahan

QQ wrote:
....
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.

I don't understand this question, because a method-local inner class can
access instance fields declared in the surrounding class.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top