4

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 the java compiler oblige me to put the worker object as a final?

Maroun
  • 94,125
  • 30
  • 188
  • 241
BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42
  • possible duplicate http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – Oliver Watkins Feb 17 '14 at 08:07
  • I don't think there's a reason, other than that Java sort of sucks. The compiler could make `worker` `final` implicitly, but it doesn't. – Chris Martin Feb 17 '14 at 08:07

0 Answers0