Java Magazine, September/October 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 79 fix this context and therefore a static method in the place described in this question cannot use this Now given that the unqualified variable y in the example static method is implicitly a reference to this y its clear that such a form of reference cannot work Given this observation options D and E are incorrect One thing you know for sure is that this y and this x must fail The next consideration is whether the static method can gain access to instance variables Such access is simple and all thats needed is a valid reference to an instance Although the this reference is not available a reference to an instance can easily be passed into the static method through its argument list So suppose the following method were inserted at line n1 public static void doStuff Sub self System out println x is self x y is self y In this case the behavior is to print the values of the x and y fields of the object passed as the actual parameter to the invocation of doStuff Another possibility is that a static method can create an instance of the class to which it belongs this is common behavior and forms the basis of the static factory concept In other words option C is precisely correct the only requirement for access to the fields is a viable reference As a result options A and B which suggest that there is no possibility of accessing these fields are incorrect The infrastructure of the get method must handle any exceptions from any Callable regardless of whether they actually exist in a particular implementation Another observation is that the suggestion in option B that the usability of the field might depend on whether its in the class or parent class is an irrelevant distraction Such a restriction could exist if the field in the parent class were private for example But thats not the case here There is also a restriction on a child class accessing a parent field if the target field has default access and the child class is in a diferent package However in this case no information is given about packaging and the exams guidance says the following
You must have JavaScript enabled to view digital editions.