Java Magazine, September/October 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 60 fix this c one one b one a two two c d one one b one a two two c two t2 txt e two t2 txt Question 1 The correct answers are options A and D The first decision is probably to determine whether the code compiles or whether the change outlined in option B is necessary This one is interesting because it depends critically on the version of Java in use When a method local variable is referenced from a class or lambda expression enclosed in the methods scope a potential problem arises A method local variable ceases to exist when the method returns However the lifetime of the object created by a nested class or lambda is potentially much longer Javas particular solution to this situation sometimes called a closure or a captured variable is to insist that the method local variable must never be changed Given that restriction its possible to simply take a copy of the variable and embed it in the longer lived object If the variable cant change a copy is as good as the original From Java 11 which introduced nested classes through Java 7 any method local variable accessed from a nested class had to be labeled final This means that option B would be correct for Java 7 However Java 8 changed the requirements a little and the keyword final is no longer required although the variable must be treated as though final were present It turns out that the compiler has had the ability since the beginning to determine if the rules of final are properly adhered to thats been important because the compiler must issue an error if you try to change a final variable Also a final variable can permit some optimizations in the generated code and the compiler has been doing neat tricks using this for some time Of course those optimizations can be done based on a variable behaving like a final not on whether the programmer labeled the variable as such The compiler has a notion of efectively final the programmer didnt say its final but it is so the compiler treats it as such As mentioned Java 8 changed the rules for those method local variables Today they must be effectively final but they do not have to be marked as such Consequently the code does in fact compile in a Java 8 environment and this article is written about the Java 8 certification exam So option A is correct and option B is not Option C builds on this question of finality What happens if the code if vals 0 0 vals i 0 is added at line n1 Well vals is a pointer to an array and the pointer is not changed by this so vals remains effectively final This is an important point about final ness for variables and indeed one of the key reasons that Java 11 added the final restriction A final variable that is of reference type cannot be changed to point at a diferent object but the contents of the object or array in this case are not protected by the final keyword so not much of a restriction is being imposed Immutable objects such as Integer and String are special not because final makes any special considerations but because Answers
You must have JavaScript enabled to view digital editions.