I want to initialize a final field in different ways. Therefore I have created an enumeration type and perform a switch over it.
public enum TYPE { A, B, } I have also added a default case to the switch with an assertion to warn my fellow programmers in case they add a new enumeration constant and forget to update the switch.
default: assert false : "missing TYPE detected"; Java however detects the flaw in my argument and complains that the blank field may not have been initialized. How should I deal with this situation?
public class SwitchExample { public enum TYPE { A, B, } private final int foo; public SwitchExample(TYPE t) { switch (t) { case A: foo = 11; break; case B: foo = 22; break; default: assert false : "missing TYPE detected"; } // The blank final field foo may not have been initialized } }
default