R
Roedy Green
I have using Intellij. One thing it often does in make suggestions on
how to improve your code. One it made, I thought was illegal till I
tried it out.
If you have code like this:
case 1:
int x = expression;
break;
case 2:
x = expression;
break;
that is LEGAL even though program flow does not flow through the
definition. You DON'T have to promote x out of the switch. You can
think of it that it works AS IF you always fell through each case. The
key to this mystery is understanding that local variables are not
actually allocated where you define them. They all get allocated at
once when you enter the method by reserving N stack slots, where they
effectively became extra dummy parameters to your method.
how to improve your code. One it made, I thought was illegal till I
tried it out.
If you have code like this:
case 1:
int x = expression;
break;
case 2:
x = expression;
break;
that is LEGAL even though program flow does not flow through the
definition. You DON'T have to promote x out of the switch. You can
think of it that it works AS IF you always fell through each case. The
key to this mystery is understanding that local variables are not
actually allocated where you define them. They all get allocated at
once when you enter the method by reserving N stack slots, where they
effectively became extra dummy parameters to your method.