Java Magazine, September/October 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 61 fix this they are intrinsically immutable Anyway in this case vals is still effectively final only the contents of the array are changed and adding the code is successful without any other changes needed Hence option C is false In fact the preceding argument shows that option D is also true The code can be added and no changes are needed By the same token however option E is false Assigning a new value to vals that is pointing vals at a different array although its a copy of the original which might seem like a good thing in general is sufficient to break the efectively final requirement and this code will not compile Question 2 The correct answer is option D This is a question with less depth and complexity than many The terminology sometimes can be confusing but to be frank that terminology isnt really very important Java uses the term nested class to describe classes defined inside other classes So Wrapped is a nested class However such a class can be labeled static If its static then just like any other static element its associated with the class as a whole rather than with any specific instance On the other hand a nested class that is not labeled static is properly called an inner class though as I suggested the terminology is commonly used very loosely and no exam question would test such a distinction of terminology What matters however is that a nonstatic nested class that is an inner class in the oficial sense actually has direct access through a hidden reference that acts similarly to this to the fields of a particular instance of the enclosing class Its as if the inner class is part of the enclosing instance Of course that means that when the instance of the inner class is created there must be an enclosing instance for it to belong to This is why option D turns out to be the required syntax The general approach is that there must be some kind of prefix for the new operation The prefix can be explicit as it is here first you build the instance of Wrapper and then you invoke new upon that instance to create the Wrapped object within The operation could be done in two stages If myWrapper were a variable of type Wrapper you could initialize myWrapper and then use myWrapper new Wrapped However in this case it was done all at once Option A isnt true you can create instances outside of Wrapper That said its probably much more common and often a better design to create the inner classes inside the enclosing class using something along the lines of a factory method Its even likely that the inner class might be private and known to the outer world only by some public interface that it implements While acknowledging that there are of course special cases the general notion of encapsulation suggests that fiddling with the inner class should mostly be done by the outer one The bottom line for this question though is that option A is incorrect All the other syntax oferings are incorrect for the purpose at hand Option B would work if Wrapped were a nested class but not if it were an inner class That is if Wrapped were a static class then option B would be correct But because the conditions are not so its incorrect Option C is not a valid construction of anything It could be valid as an invocation of a method called Wrapped thats a member of the Wrapper class Such a method contradicts normal Java style conventions which reserve capital first letters for classes and interfaces and call for lowercase letters for methods However its actually possible for that method to coexist with the class of the same name creating a situation where option C could compile and return a new object of Wrapped type However thats not going to work unless the method is added to the class and because the entire class is shown in the example you can rule out this line of reasoning which is based on some crazy inappropriate broken style conventions Option E is just made up syntax Its not a method reference because it includes parentheses It doesnt have any context for a lambdas type to be determined to support the
You must have JavaScript enabled to view digital editions.