Hello! Why the code below does not work in java 7 (compiler warning), but does it work in java 8?

interface Destination{ String readLabel(); } public class Parcel9 { // Argument must be final to use inside // anonymous inner class: public Destination destination(String dest) { return new Destination() { private String label = dest; public String readLabel() { return label; } }; } public static void main(String[] args) { Parcel9 p = new Parcel9(); Destination d = p.destination("Tasmania"); System.out.println(d.readLabel()); } } 
  • one
    What warning? - VladD
  • It is possible to end it - Bill
  • one
    @VladD, in the text of the question is: Argument must be finalized to use inside anonymous inner class: - Grundy
  • Yeah, I saw. Good question then. - VladD

1 answer 1

In 8 java, the compiler itself calculates whether the variable you used is actually final (that is, you are not trying to assign something to it) and allows you to compile it, implicitly substituting final

There is no such mechanism in the 7th of January and you need to declare the argument as final in order for the code to be compiled.

  • that is, if you add an assignment to a function in a function, does java8 also curse? - Grundy
  • one
    @Grundy, as it is not strange, but it seems that it will be so) - Yuriy SPb