0

What is wrong with this code? I am trying to have it go to the next sorted array named bk when I click on the button.

Book[] bk = new Book[5];
bk = sortArray(bk);
final JTextArea textArea = new JTextArea();

JButton btnNext = new JButton("Next");
btnNext.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        for (int i = 0; i < bk.length; i++)
            textArea.append("\n" + bk[i+1]);
    }});

But if I declare it as final the line that sorts it tells me to take the final declaration out.

Here the error with the final declaration:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The final local variable bk cannot be assigned. It must be blank and not using a compound assignment

And w/o the final declaration:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
Cannot refer to a non-final variable bk inside an inner class defined in a different method
Cannot refer to a non-final variable bk inside an inner class defined in a different method

What can I do to fix this problem?

2 Answers 2

3

Try this:

Book[] bk = new Book[5];
// fill bk here
final Book[] bkSorted = sortArray(bk);
Sign up to request clarification or add additional context in comments.

Comments

0
  1. You're trying to run code that doesn't compile. You don't want to do that but instead will want to fix all compilation issues at the compile stage of development.
  2. You can make bk a non-final instance field of the class and not a local variable, and the issue is solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.