Java Reference
In-Depth Information
< Day Day Up >
Puzzle 69: Fade to Black
Suppose that you can't modify classes X and C in the previous puzzle ( Puzzle 68 ). Can you write a
class whose main method reads the value of the field Z in class X.Y and prints it? Do not use
reflection.
Solution 69: Fade to Black
At first, this puzzle may appear impossible. After all, the class X.Y is obscured by a field of the
same name, so an attempt to name it will refer to the field instead.
In fact, it is possible to refer to an obscured type name. The trick is to use the name in a
syntactic context where a type is allowed but a variable is not. One such context is the region
between the parentheses in a cast expression. The following program solves the puzzle by using this
technique and prints Black as expected:
public class FadeToBlack {
public static void main(String[] args){
System.out.println(( (X.Y) null).Z);
}
}
Note that we are accessing the Z field of class X.Y by using an expression of type X.Y . As we saw in
Puzzles 48 and 54 , accessing a static member using an expression in place of a type name is a legal
but questionable practice.
You can also solve this puzzle without resorting to questionable practices, by using the obscured
class in the extends clause of a class declaration. Because a base class is always a type, names
appearing in extends clauses are never resolved as variable names. The following program
demonstrates this technique. It too prints Black :
 
 
Search WWH ::




Custom Search