0

I am creating a Medical Shop Billing software in which I have three JFrames one of which I need to setVisible(false) on the click a JMenuItem. However each time I compile an error is displayed

JFrame f is accessed from within inner class; needs to be declared final 

Can anyone please help me on this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Richik Chanda
  • 23
  • 1
  • 7

2 Answers2

1

Inner local class object is created in the context of the outer class object, if you refer a outer class Object to your inner class What happens when a method terminates and the value changes during execution of the method? Those local variables should be kept in existence although they are local variables.

Solution is that you have to make those local variable final. It is an indication to the compiler that that variable should be copied to somewhere else (if required) when the method itself terminates.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

If you need to access variable on outer class. You need to add final like this:

final public JFrame frame

You can read here for more information final (Java) on Wikipedia.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jeff Lee
  • 783
  • 9
  • 17