Java complains about final field not initialized in default case of a switch -
i want initialize final field in different ways. therefore have created enumeration type , perform switch on it.
    public enum type {         a,         b,     }   i have added default case switch assertion warn fellow programmers in case add new enumeration constant , forget update switch.
        default:             assert false : "missing type detected";   java detects flaw in argument , complains blank field may not have been initialized. how should deal 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";         }          // blank final field foo may not have been initialized     } }      
instead of assert false : "missing type detected"; may throw illegalargumentexception("missing type detected")
Comments
Post a Comment