Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would a program compile for me, but not for another person?

My code is below. It compiles fine for me, however my professor is saying he's getting an error because I don't have a few variables in my class declared final. Eclipse doesn't seem to have a problem with this on my machine so I don't know how to fix what I can't see is wrong.

I understand that some variable need to be final to work in nested classes , but the ones I've created seem to work just fine, final or not.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

public class JColorFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JColorFrame frame = new JColorFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public JColorFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 522, 339);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.WEST);

        JPanel panel_2 = new JPanel();
        contentPane.add(panel_2, BorderLayout.EAST);

        JPanel panel_3 = new JPanel();
        contentPane.add(panel_3, BorderLayout.SOUTH);

        Color[] color = new Color[8];
        color[0] = Color.black;
        color[1] = Color.white;
        color[2] = Color.red;
        color[3] = Color.blue;
        color[4] = Color.green;
        color[5] = Color.yellow;
        color[6] = Color.magenta;
        color[7] = Color.orange;

        JButton btnChangeColor = new JButton("Change Color");
        btnChangeColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Random random = new Random();
                int random_1 = random.nextInt(4);
                switch (random_1) {
                    case 0:
                        int random_2 = random.nextInt(8);
                        panel.setBackground(color[random_2]);
                        break;
                    case 1:
                        random_2 = random.nextInt(8);
                        panel_1.setBackground(color[random_2]);
                        break;
                    case 2:
                        random_2 = random.nextInt(8);
                        panel_2.setBackground(color[random_2]);
                        break;
                    case 3:
                        random_2 = random.nextInt(8);
                        panel_3.setBackground(color[random_2]);
                        break;

                }

            }
        });

        contentPane.add(btnChangeColor, BorderLayout.CENTER);
    }
}
like image 355
KAT Avatar asked Jun 07 '15 03:06

KAT


1 Answers

Inner classes, such as your ActionListener, cannot access non-final variables from the scope that contains it. In Java versions less than 8, the compiler will automatically throw an error if it encounters this situation.

In Java 8, the compiler will simply make the variables final if they are not changed anywhere in the file, as yours are not here.

For more information, see this post.

Probably, your professor is using Java 7 or older, so your code is not compiling. Just make any local variables you use in your inner class final.

like image 127
k_g Avatar answered Oct 13 '22 22:10

k_g