Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing variable of an outer class from an inner class

 
Liza Boa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also the option of declaring the local variable as final, like the compiler says.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic