Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why method local inner class can use final variable rather than...

Reply
Thread Tools

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

 
 
QQ
Guest
Posts: n/a
 
      05-01-2008
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
 
Reply With Quote
 
 
 
 
QQ
Guest
Posts: n/a
 
      05-01-2008
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
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      05-01-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Local variables used but not declared in an inner class must bedeclared final. Philipp Java 3 11-26-2008 10:26 PM
why inner class can use outer class function ? Ahan Hsieh Java 2 10-05-2007 09:03 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
question on how to use local inner class inside a static method Shawn Java 4 10-13-2006 02:14 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57