java - Why am I able to access enum constants directly as case labels of a switch statement in another class -
why able access enum constant hot directly in code below:
enum spice_degree { mild, medium, hot, suicide; } class switchingfun { public static void main(string[] args) { spice_degree spicedegree = spice_degree.hot; //unable access hot directly switch (spicedegree) { case hot: //able access hot directly here system.out.println("have fun!"); } } }
it's way language defined - when case labels enum values, means switch
expression must enum of type, specifying enum type everywhere redundant.
from jls 14.11:
every case label has case constant, either constant expression or name of enum constant.
and
if type of switch statement's expression enum type, every case constant associated switch statement must enum constant of type.
Comments
Post a Comment