Consider this:
class BlackMage {
public void castSpell(SPELL_TYPE spellType) {
Spell spell;
if (spellType == null)
return;
switch(spellType) {
case FIRE:
spell = new Fire();
break;
case BLIZZARD:
spell = new Blizzard();
break;
case THUNDER:
spell = new Thunder();
break;
}
// use spell
}
enum SPELL_TYPE {
FIRE,
BLIZZARD,
THUNDER
}
}
Unless I add a default
block to the switch
and initialize spell there, the compiler will complain about spell not being initialized. I don't understand why, since we handled all 3 possibilities.