0

How can I change the label of a JRadioButton when it is set as final?

This is a section of code from the constructor where the radio buttons are initialised, when the check button is pressed, the selected radio button's value is sent to a method which verifies the user's answer:

final JRadioButton ANSWER1 = new JRadioButton("Empty 1");
final JRadioButton ANSWER2 = new JRadioButton("Empty 2");
final JRadioButton ANSWER3 = new JRadioButton("Empty 3");
final JRadioButton ANSWER4 = new JRadioButton("Empty 4");

JButton CHECK = new JButton("Check");
CHECK.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        if (ANSWER1.isSelected()){
            qAnswered(1);
        }
        if (ANSWER2.isSelected()){
            qAnswered(2);
        }
        if (ANSWER3.isSelected()){
            qAnswered(3);
        }
        if (ANSWER4.isSelected()){
            qAnswered(4);
        }

    }   

});

When the next question button is selected, the text labels of the radio buttons need to change to the next set of answers. Unfortunately, because the radio buttons are set to final this causes a the application to crash.

How can I proceed?

1 Answers1

3

The final keyword for a variable means that the variable can be initialized only once, but that doesn't mean that the fields in that object can't be changed. Even though JRadioButton references are final, ANSWER4.setText() would never throw any final related error.

if (ANSWER1.isSelected()){
    qAnswered(1);
    ANSWER1.setText("New Text 1"); // This will work
}

For more clarity on final variables, from the JLS 4.12.4(emphasis mine)

A variable can be declared final. A final variable may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

A blank final is a final variable whose declaration lacks an initializer.

Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.

Community
  • 1
  • 1
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Thanks for the response. We're actually getting a null pointer exception "when ANSWER1.setText("blah blah...");" is called. Would you be able to help with this instead? – chrispollock Apr 03 '14 at 05:31
  • I don't see why it should throw a NPE, unless the `JRadioButton` aren't initialized when `ANSWER1.setText()` is executed. – Rahul Apr 03 '14 at 05:33
  • Neither do we, it happens whenever the nextQuestion() method is called which contains ANSWER1.setText... – chrispollock Apr 03 '14 at 05:41
  • Probably the `ANSWER1` you're accessing the in the `nextQuestion()` method isn't initialized? – Rahul Apr 03 '14 at 05:50