<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>
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?
> >>I was told that the variable accessed inside a anonymous inner class
> >> needs to be static.
[quoted text clipped - 6 lines]
> class (and can only call static methods of that class); pehshps that's what
> the OP was thinking of.
Stefan Ram - 18 Jul 2006 01:02 GMT
><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( ).
First, objects are not "defined": Objects are created at
run-time and identifiers are declared at compile-time in Java.
Also, finality is not an attribute of references but of
identifiers.
I can not understand why people recommend TIJ from this
quotation (I never read more of it).
IchBin - 18 Jul 2006 06:21 GMT
>> <Quote from TIJ3 chapter 8>
>> If you're defining an anonymous inner class and want to use an object
[quoted text clipped - 10 lines]
> quotation (I never read more of it).
>
Yes, drop "TIJ". You should be reading a book like say: 'Effective Java:
Programming Language Guide' by Joshua Bloch.
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Oliver Wong - 20 Jul 2006 18:46 GMT
> First, objects are not "defined": Objects are created at
> run-time and identifiers are declared at compile-time in Java.
> Also, finality is not an attribute of references but of
> identifiers.
I'm guessing you mean "Also, finality is not an attribute of objects,
but of identifiers/references".
- Oliver
Mike Schilling - 18 Jul 2006 01:36 GMT
> <Quote from TIJ3 chapter 8>
> If you're defining an anonymous inner class and want to use an object
[quoted text clipped - 3 lines]
> 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.
> Though a *static* inner class can only access static field of its enclosing
> class (and can only call static methods of that class); pehshps that's what
> the OP was thinking of.
FWIW, "static inner class" is an oxymoron. You might mean "static nested
class". http://jqa.tmorris.net/GetQAndA.action?qids=67&showAnswers=true

Signature
Tony Morris
http://tmorris.net/
Mike Schilling - 18 Jul 2006 16:34 GMT
>> Though a *static* inner class can only access static field of its
>> enclosing
[quoted text clipped - 4 lines]
> FWIW, "static inner class" is an oxymoron. You might mean "static nested
> class". http://jqa.tmorris.net/GetQAndA.action?qids=67&showAnswers=true
Yes, that distinction is made at the top level. But what about a local or
anonymous class defined in a static method? I've never seen one of those
called a "nested class".
In other words, the terminology is fouled up. There are really six
categories of classes defined within other classes, defined along two axes:
Instance Static
Class-level 1 2
Local 3 4
Anonymous 5 6
For those defined at the class-level, "static" is a keyword. For those
defined withing methods, it depends on whether they occur withing instance
or static methods. The odd-numbered ones have encosing instances; the
even do not.
Now, what would you call 4 and 6, if not "static inner classes"? For that
matter, why call everything besides 2 an "inner class", when the real
distinction is odd vs. even?