0

I am making Java Swing application. i made check box item, then when i was calling show.isSelected() method, i got an error that you have to make show final, then i made it final and the error was gone, the error was in the 9th line, but i did not understand that what was the reason for making show final and what final does with show ?

    final JCheckBoxMenuItem show=new JCheckBoxMenuItem("Show");
    show.setSelected(true);
    show.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0) 
        {

            formpanel.setVisible(show.isSelected()); ////// giving error without final

        }

    });
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Taimoor Khan
  • 53
  • 1
  • 6

1 Answers1

1

The final keyword prevents you from reassigning reference show so the compiler can safely copy its value into new anonymous object created by new ActionListener statement.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105