Why is it mandatory to use a variable in internal functions?

I have the following code:

public static void main(String[] args) {

    final AbcClass worker = new AbcClass() {

        @Override
        public void sayHello() {
            System.out.println("hello  ");

        }

    };
    JButton jButton = new JButton();
    jButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            worker.sayHello();

        }
    });

}

Why does the java compiler oblige me to put the work object as final?

+4
source share

Source: https://habr.com/ru/post/1527275/


All Articles