Re: inner class and static



<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?



Mike Schilling wrote:
"Matt Humphrey" <matth@xxxxxxxxxxxxxx> wrote in message
news:QcKdnauD-daeiyHZnZ2dnUVZ_s-dnZ2d@xxxxxxxxxxxxxxx

"jim" <jseers@xxxxxxxxx> wrote in message
news:1153161800.679433.165030@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I was told that the variable accessed inside a anonymous inner class
needs to be static.

An inner class (whether anonymous or not) may access the instance
variables of the outer class and it can access the local variables of the
method that instantiates it, provided they are final.


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.

.



Relevant Pages

  • Re: Signed applet strange behaviour
    ... By using 'new ItemListener() ' you tell the compiler to create an ... anonymous inner class, contained in file Test$1.class. ... missed to include this Test$1.class file in your Test.jar (as Andrew has ...
    (comp.lang.java.programmer)
  • Re: Signed applet strange behaviour
    ... By using 'new ItemListener() ' you tell the compiler to create an ... anonymous inner class, contained in file Test$1.class. ... missed to include this Test$1.class file in your Test.jar (as Andrew has ...
    (comp.lang.java.programmer)
  • Re: Updating GUI & Running Program (AND accessing common variables!)
    ... >>have access to final locals with an anonymous inner class. ... You need to clarify what you mean by 'in the outer class'. ...
    (comp.lang.java.gui)
  • Re: inner classes and other stuff...
    ... >> An anonymous inner class is an inner class that is declared, ... >> single instance of the class created, ... >> as the only declaration in the body of the class. ...
    (comp.lang.java.help)
  • Re: Updating GUI & Running Program (AND accessing common variables!)
    ... >> outer class from the inner class, I get a compile error that says I can't ... > You need to clarify what you mean by 'in the outer class'. ... > make a long story short, an anonymous inner class can only access local ... Okay, let me rephrase, to make sure I understand this correctly. ...
    (comp.lang.java.gui)