The following code does not compile because eater
is defined twice:
switch (vegetable) {
case TOMATO:
Eater eater = new Eater(Tomato.class, many parameters);
eater.eat(more parameters);
return true;
case POTATO:
Eater eater = new Eater(Potato.class, many parameters);
eater.eat(more parameters);
return true;
case CARROT:
doSomethingElse();
return true;
}
Should I:
- Use separate variables `tomatoEater` and `potatoEater`, making the code less maintainable?
- Define `eater` before the `switch`, making it accessible to more than it should?
- Define `eater` the first time only, leading to potential confusion?
- Add braces, making the code more verbose?
- Any better idea?