wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes accessing variable of an outer class from an inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing variable of an outer class from an inner class" Watch "accessing variable of an outer class from an inner class" New topic
Author

accessing variable of an outer class from an inner class

Liza Boa
Greenhorn

Joined: Aug 16, 2001
Posts: 23
Hi there
I have an (ButtonManager)object that contains a vector. this ButtonManager calls a GUI to start with an instance of itself. The GUI has a few buttons; their actionListeners are in an inner class. Every time a button is hit, the actionPerformed-method has to search in the vector the demanded element and to modify it. My problem is that I cannot access the vector out of this inner class. (Compiler says: local variable bm is accessed from within inner class; needs to be declared final.)
Can somebody help?
Thanks, Liza
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I can see several possibilities for this:
How about the "self shunt" pattern. Don't other with the inner classes, implement the listeners directly in the manager class, and decide what to do on which button the click was for (it's in the Event).
How about making the listeners "real" classes, and passing the Vector into the Constructor?
Are you sure you really need to search the whole Vector in each listener? If every action causes a button listener to search for the same element in the vector, why not pass in something else (the element itself, the vector and the index of the element in the Vector) to the constructor?
Have any of these options helped ?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There's also the option of declaring the local variable as final, like the compiler says.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing variable of an outer class from an inner class