0
public Destination destination(final String dest, final float price) {
    return new Destination() {
        private int cost;
        {
            cost = Math.round(price);
            if (cost > 100)
                System.out.prinltn("Over budget!");
        }
        private String label = dest;
        public String readLabel() { return label; }
    };
}

Parameters used by the contained anonymous inner class are modified by "final". But why?

Zhou Tai
  • 214
  • 1
  • 10

1 Answers1

1

Regarding to the rule applies to inner classes (JLS 8.1.3):

Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final. Any local variable, used but not declared in an inner class must be definitely assigned before the body of the inner class.

Read more here

Community
  • 1
  • 1
Salah
  • 8,567
  • 3
  • 26
  • 43