0

So right now I have the following:

public class Panel extends JPanel {

int size;

public Panel()
{
    JButton newBut = new JButton();

    newBut.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            boolean validInput = false;
            while (!validInput)
            {
                String input = JOptionPane.showInputDialog("Please specify a number:");
                try{
                    size = Integer.parseInt(input);
                    validInput = true;
                } catch (NumberFormatException ex){
                    JOptionPane.showMessageDialog(new JFrame(), "Invalid Input!", "ERROR",JOptionPane.ERROR_MESSAGE);
                }

                if (input == null)
                {
                    validInput = true;
                }
            }
        }
    });
}

Now as you can see from the code, I am trying to parse the input from the user as an integer, and then store that value inside my global variable "size". However, when I try to do this I get the error:

Cannot refer to a non-final variable inside an inner class defined in a different method

Setting size to final is not an option for me, since I need size to be able to change each time the user inputs a new value. So I honestly have no idea how I'm supposed to retrieve the size variable from the inner method. Any help would be mightily appreciated.

user207421
  • 305,947
  • 44
  • 307
  • 483
Gribbles Chan
  • 123
  • 1
  • 3
  • 9
  • hmm, I like these types of bugs. Thanks actually I need to brush up on this. The whole static vs. non-static & final vs non-final. access privileges & such . It's very very important to grok it – Caffeinated May 15 '14 at 02:05
  • a good source - http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen?rq=1 – Caffeinated May 15 '14 at 02:06
  • 1
    Cannot reproduce. Your code compiles for me. Java 1.7.0. The error message applies to a method-local variable, not a class member. Clearly this isn't the real code. – user207421 May 15 '14 at 02:48

2 Answers2

0

Yes @EJP is right....I have also tried in Java 8 using netbeans...for me it is working perfectly...but if still you get problem then implement ActionListener to your class and then make a separate method for this...here is the code....

public class Panel extends JPanel implements ActionListener{

    int size;

    public Panel()
    {
        JButton newBut = new JButton("Button");  //give some name just to check..
        newBut.addActionListener(this);
        this.add(newBut);  //you have not added your button to the Panel..so I did it
    }

    @Override
    public void actionPerformed(ActionEvent e) {  //same method just outside the inner class
        boolean validInput = false;   //all the functionality is same as your nothing is changed
        while (!validInput)
        {
            String input = JOptionPane.showInputDialog("Please specify a number:");
            try{
                size = Integer.parseInt(input);
                validInput = true;
            } catch (NumberFormatException ex){
                JOptionPane.showMessageDialog(new JFrame(), "Invalid Input!", "ERROR",JOptionPane.ERROR_MESSAGE);
            }

            if (input == null)
            {
                validInput = true;
            }
        }
    }
    }

I think it may help you...

Ankit
  • 138
  • 7
-1

You could cheat and use an AtomicInteger or similar, and call its set(int) method. But I would instead reconsider your design -- why are you trying to stash this value in a global anyways?

Also, what's with the floating

if (input == null)
{
    validInput = true;
}

block?

Thorn G
  • 12,620
  • 2
  • 44
  • 56
  • Yes you can. I do it all the time. What you can't do is set a method-local variable in the enclosing method. That doesn't appear in this question. – user207421 May 15 '14 at 02:27
  • D'oh, you're right -- I misread the OP's question based on the quoted compiler message. – Thorn G May 15 '14 at 12:34