Re: inner class and static




"jim" <jseers@xxxxxxxxx> wrote in message
news:1153180095.054378.243480@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<Quote from TIJ3 chapter 8>
If you're defining an anonymous inner class and want to use an object
that's defined outside the anonymous inner class, the compiler
requires that the argument reference be final, like the argument to
dest( ). If you forget, you'll get a compile-time error message.
Feedback
</Quote>

Is that really what it says? That's awful. What it means to say is that
local and anonymous classes can access local variables and formal parameters
that are visible in their enclosing block. For this to work, these variables
must be declared final. (Paraphrased from JLS 3, sec 8.1.3)

The txt mentioned in the original post is an object that's defined
outside the anonymous inner class, but still the compiler didn't
complain, why?

It's a field in the enclosing class, not a local variable, so final isn't
necessary.


.



Relevant Pages

  • Re: inner class and static
    ... If you're defining an anonymous inner class and want to use an object ... that's defined outside the anonymous inner class, the compiler ... requires that the argument reference be final, ... run-time and identifiers are declared at compile-time in Java. ...
    (comp.lang.java.programmer)
  • Re: Why final?
    ... To add to this (and I wish I could find a reference, ... Determine which non-final local variables in a method are written ... the creation of some anonymous inner class that reads that variable. ... Generate field accesses instead of local variable accesses at the ...
    (comp.lang.java.help)
  • Re: Why must and must not be "final" ?
    ... It's an ugly but effective workaround when an anonymous inner class ... needs access to local variables or parameters that you want to change. ...
    (comp.lang.java.programmer)
  • Re: Why must and must not be "final" ?
    ... It's an ugly but effective workaround when an anonymous inner class ... needs access to local variables or parameters that you want to change. ...
    (comp.lang.java.programmer)