简体   繁体   中英

“Cannot refer to the non-final local variable defined in an enclosing scope.” after changing Execution Environment

I have a problem with my Java code in Eclipse. When I was developing my program, there was always a warning: "Build Path specifies execution environment JAVA SE 1.7...", and finding it odd, I searched for it here, and got to the conclusion that I had to remove JRE System Library [JSE-1.3] , and add a more recent one, which I did.

However, after applying the new libraries, all of my three under-development files instantly got lots of errors warnings. Not understanding what the actual problem was, I tried to return the old libraries to my project's properties. But after doing it, although around 90% of errors went away, there were still some left. Stating:

"Cannot refer to the non-final local variable defined in an enclosing scope." And it is wanting me to put final on every single variable that I use with the ActionListener or MouseListener. The new warning message is:

"Build path specifies execution environment J2SE-1.3. There are no JREs installed in the workspace that are strictly compatible with this environment."

Here is the code, I am sorry if I did a mistake, but I am new to the programming language, and I can't find a solution for this. The code is simplified to a point it won't work with only that, showing only an example.

public class MainGUI {

    public static void main(String[] args) throws IOException {

        JFrame mainFrame = new JFrame("Physics Simulator");

            JButton exitButton = new JButton(" Exit ");
        exitButton.setVerticalAlignment(SwingConstants.TOP);
        exitButton.setBounds(331, 15, 80, 24);
        exitButton.setForeground(Color.BLACK);
        exitButton.setFont(new Font("Calibri", Font.PLAIN, 17));
        exitButton.setFocusPainted(false);
        exitButton.setBackground(SystemColor.scrollbar);

            JCheckBox exitAskingcheck = new JCheckBox("Exit without asking");
        exitAskingcheck.setSelected(false);
        exitAskingcheck.setFont(new Font("Verdana", Font.PLAIN, 11));
        exitAskingcheck.setBackground(SystemColor.scrollbar);
        exitAskingcheck.setBounds(775, 17, 133, 23);
        exitAskingcheck.setFocusPainted(false);

        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent e) {

                if ( exitAskingcheck.isSelected() ) {
                System.exit(0); } else {

                    JFrame exitAsk = new JFrame("Physics Simulator");
                    exitAsk.getContentPane().setBackground(Color.BLACK);
                    exitAsk.setBackground(Color.BLACK);
                    exitAsk.setSize(400,200);

                    JButton no = new JButton("Not yet...");
                    no.setFont(new Font("Verdana", Font.PLAIN, 11));
                    no.setFocusPainted(false);
                    no.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {

                            exitAsk.setVisible(false);
                            exitAsk.removeAll();
                            exitAsk.setEnabled(false);

                        }
                    });

                    JButton yes = new JButton("Yes");
                    yes.setFont(new Font("Verdana", Font.PLAIN, 11));
                    yes.setFocusPainted(false);
                    yes.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            System.exit(0);
                        }
                    });

                    JLabel askText = new JLabel("Are you sure you want to leave?");
                    askText.setForeground(Color.WHITE);
                    askText.setFont(new Font("Verdana", Font.PLAIN, 15));
                    exitAsk.getContentPane().setLayout(null);
                    exitAsk.getContentPane().add(no);
                    exitAsk.getContentPane().add(yes);
                    exitAsk.getContentPane().add(askText);
                    no.setBounds(220,100,100,30);
                    yes.setBounds(40,100,100,30);
                    askText.setBounds(64,27,250,30);

                    exitAsk.setVisible(true);
                    exitAsk.setAlwaysOnTop(true);
                    exitAsk.setLocationRelativeTo(null);
                    exitAsk.setResizable(false);

                }
            }
        });

The error message is given at the variable exitAskingcheck on:

if ( exitAskingcheck.isSelected() ) {

And also at each exitAsk on:

          JButton no = new JButton("Not yet...");
        no.setFont(new Font("Verdana", Font.PLAIN, 11));
        no.setFocusPainted(false);

        no.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                    exitAsk.setVisible(false);
                    exitAsk.removeAll();
                    exitAsk.setEnabled(false);

            }
        });

EDIT: I fixed the "Build path" warning by installing the default workspace library (I did not install it before because... Stupidness). However, it is still asking me to classify the mentioned variables as final . Before making all those changes to properties, they were never needed. I could run both Action and Mouse Listeners with no problem, now it seems they do not recognize correctly the variables when mentioning them inside a Listener.

My question is: As they did not ask that before, do I need to put final on every variable, or is there a way to return back to "normal"?

Do not use the old version, too old, try to use a newer JDK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM