local variable is accessed within inner class (java)

55,742

Solution 1

You need to declare

JTextField input = new JTextField("",10);

and

JComboBox c_age = new JComboBox(age);

like this:

final JTextField input = new JTextField("",10);

final JComboBox c_age = new JComboBox(age);

This means that input and c_age cannot change:

Any local variable, used but not declared in an inner class must be definitely assigned before the body of the inner class.

Explanation taken from The Java Language Specification, Section - 8.1.3 Inner Classes and Enclosing Instances

Solution 2

If you declare the variables as an final then it will solve your errors but according to me its not the good solution for the problem. Similar problem has discussed here you can have a look here for more understanding.

In solution to yours problem you can have define methods by using them you can get better solution. For hint you can read How to access non-final local variable inside anonymous inner class

Solution 3

Any variable that you use inside the actionPerformed method of your inner class will need to be declared final. Try the following:

import javax.swing.*;
import java.awt.event.*;

    public class GUI
    {
        public static void main(String[] args)
        {
            JFrame frame = new JFrame("Try GUI");
            JLabel l1 = new JLabel("Please Enter Your Child's Name");
            final JTextField input = new JTextField("",10);

            JLabel l2 = new JLabel("Choose Your Child's Age");
            String[] age = {"Age","1","2","3","4","5","6"};
            final JComboBox c_age = new JComboBox(age);

            JButton button = new JButton("Search");

            JTextArea result = new JTextArea();
            JScrollPane extend_area = new JScrollPane(result);

            button.addActionListener(new ActionListener()
            {
                    public void actionPerformed(ActionEvent ae)
                    {
                        String name = input.getText();
                        Object child_age = c_age.getSelectedItem();


                    }
            });

            JPanel panel = new JPanel();
            panel.add(l1);
            panel.add(input);
            panel.add(l2);
            panel.add(c_age);
            panel.add(button);
            panel.add(extend_area);
            frame.add(panel);
            frame.setSize(350,350);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }

    }

Solution 4

As adding final takes away a lot of flexibility, I'd like to suggest the following: create an accessor method, which is encouraged anyway. But this is mostly useful when dealing with objects, while in your case everything is static. Therefore, this answer might not apply to your specific situation, but because googling for your error message yields this question as the top result, I think an alternative that's applicable in most cases (using objects is more common than doing everything from a static method) should be present here as well.

public class MyClass extends MySuperClass {
    private MyPropertyClass aProperty;

    public MyClass() {
        new JButton().setActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // aProperty.aMethod(); -- Bang! That should be final, the compiler says.
                getMyProperty().aMethod(); // -- Everything okay, works fine, no compiler complaints.
            }
        });
    }

    public getMyProperty() {
        return aProperty;
    }
}
Share:
55,742

Related videos on Youtube

Roubie
Author by

Roubie

juz open my heart for programming and software development...............

Updated on July 09, 2022

Comments

  • Roubie
    Roubie almost 2 years

    I got two errors after I compiled my code.

    The errors are:

    1.

      local variable input is accessed within inner class; 
      needs to be declared final
         String name = input.getText();
    

    2.

      local variable c_age is accessed within inner class; 
      needs to be declared final
         Object child_age = c_age.getSelectedItem();
    

    This is my code:

    import javax.swing.*;
    import java.awt.event.*;
    
    public class GUI
    {
        public static void main(String[] args)
        {
            JFrame frame = new JFrame("Try GUI");
            JLabel l1 = new JLabel("Please Enter Your Child's Name");
            JTextField input = new JTextField("",10);
    
            JLabel l2 = new JLabel("Choose Your Child's Age");
            String[] age = {"Age","1","2","3","4","5","6"};
            JComboBox c_age = new JComboBox(age);
    
            JButton button = new JButton("Search");
    
            JTextArea result = new JTextArea();
            JScrollPane extend_area = new JScrollPane(result);
    
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    String name = input.getText();
                    Object child_age = c_age.getSelectedItem();
                }
            });
    
            JPanel panel = new JPanel();
            panel.add(l1);
            panel.add(input);
            panel.add(l2);
            panel.add(c_age);
            panel.add(button);
            panel.add(extend_area);
            frame.add(panel);
            frame.setSize(350,350);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
    }
    

    How can I solve this error?

  • Roubie
    Roubie about 13 years
    problem solved.but i dont understand your explanation.hope u could give more...thanks a lot Edward
  • edwardsmatt
    edwardsmatt about 13 years
    Attached an Excerpt and Link to the Java Language Specification, I hope that helps...
  • Roubie
    Roubie about 13 years
    i have this error:non static variable cannot be referenced from a static context
  • Vincent Ramdhanie
    Vincent Ramdhanie about 13 years
    The main method is the static context. You have to break out of it by instantiating an instance of your class like in my example. The various code from your main method should then go in the constructor.
  • Chris Dennett
    Chris Dennett about 13 years
    final is needed so that the user can't change the variable when it is being used by the inner class / closure. Think about it: if it isn't marked as final, inner class references can do everything a bit of code next to the declaration can do, so you have to mark it as final to avoid concurrency problems, and the syntax enforces this.
  • Mukesh Singh Rathaur
    Mukesh Singh Rathaur about 13 years
    In other way you can declare your input and c_age variables as static instance variable that will solve your problem.
  • Seth
    Seth about 10 years
    Can you explain why it needs to be declared final?

Related