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

Discussion in 'Java' started by QQ, May 1, 2008.

  1. QQ

    QQ Guest

    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
     
    QQ, May 1, 2008
    #1
    1. Advertising

  2. QQ

    QQ Guest

    Re: Why method local inner class can use final variable ratherthan...

    On May 1, 11:10 pm, Lew <> 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
     
    QQ, May 1, 2008
    #2
    1. Advertising

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

    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
     
    Patricia Shanahan, May 1, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,996
    Smokey Grindel
    Dec 2, 2006
  3. Shawn
    Replies:
    4
    Views:
    461
    Furious George
    Oct 13, 2006
  4. Philipp
    Replies:
    3
    Views:
    1,135
    Roedy Green
    Nov 26, 2008
  5. Yohan N. Leder
    Replies:
    9
    Views:
    160
    John Bokma
    Aug 16, 2006
Loading...

Share This Page