Here is some code that sets an action listener for a button

Here is some code that sets an action listener for a button that modifies the button\'s label (like a flip-flop):

final JButton btn = new JButton(\"Disable\");

final boolean isEnabled = true; // start in enabled state

btn.addActionListener(new

  ActionListener() {

private void actionPerformed(ActionEvent ev) {

if (isEnabled) {

  btn.setText(\"Enable\");

  isEnabled = false;

}

else { // in disabled state. Swicth state:

  btn.setText(\"Disable\");

  isEnabled = true;

}

}

});

Select the statement(s) describing what is wrong with this code

a) the visibility for method  actionPerformed is more restrictive than its declaration in the ActionListener interface and that leads to compilation error

b) isEnabled local variable is \'final\' and it should not be changed from actionPerformed().

c) the btn variable is declared final, but then it is initialized in the same statement

d) the ActionEvent parameter is never used

Solution

Answer: b) isEnabled local variable is \'final\' and it should not be changed from actionPerformed().

Once we applied a final keywrd on variable then that final variable value can be reassinged in later stages. In Out case we applied a final keyword on variable isEnabled  that is assign ed iwth true that can not be reassinged ater. the value of that isEnabled  variable is final. It should always be true.

Here is some code that sets an action listener for a button that modifies the button\'s label (like a flip-flop): final JButton btn = new JButton(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site