Java Reference
In-Depth Information
< Day Day Up >
Puzzle 72: Final Jeopardy
This puzzle examines what happens when you attempt to hide a final field. What does this program
do?
class Jeopardy {
public static final String PRIZE = "$64,000";
}
public class DoubleJeopardy extends Jeopardy {
public static final String PRIZE = "2 cents";
public static void main(String[] args) {
System.out.println(DoubleJeopardy.PRIZE);
}
}
Solution 72: Final Jeopardy
Because the PRIZE field in Jeopardy is declared public and final , you might think that the
language would prevent you from reusing this field name in a subclass. After all, final methods
cannot be overridden or hidden. If you tried the program, you found that it compiles without a hitch
and prints 2 cents . What went wrong?
It turns out that the final modifier means something completely different on methods and
fields . On a method, final means that the method may not be overridden (for instance methods) or
hidden (for static methods) [JLS 8.4.3.3]. On a field, final means the field may not be assigned
more than once [JLS 8.3.1.2]. The keyword is the same, but the behavior is unrelated.
In the program, the final field DoubleJeopardy.PRIZE hides final field Jeopardy.PRIZE , for a net
loss of $63,999.98. Although it is possible to hide fields, it is generally a bad idea. As we discussed
in Puzzle 66 , hiding fields can violate subsumption and confound our intuition about the
relationship between types and their members.
 
 
Search WWH ::




Custom Search