为什么在一个 case 块中定义的变量可以在其他 case 块中使用?
可能的重复:
为什么 case 语句中的变量不是本地变量?
定义的变量作用域块中的内容不能在其外部使用。例如,以下代码片段无效:
{
int anothervar = 4;
}
{
System.out.println(anothervar);
}
但看起来 case
块不会创建单独的作用域。
switch (mode) {
case ONE:
dosomething();
return;
case TWO:
int[] someints = new int[] { 2, 3, 5, 7 };
SomeObject obj = new SomeObject();
return;
case THREE:
someints = new int[] { 1, 4, 6, 8, 9 };
obj = new SomeObject();
return;
}
为什么我不必在 case THREE
'block' 内声明 someints
?
假设 mode = THREE
,则永远不会达到变量 someints
的声明,因为 case TWO
,其中 someints
是已声明,则被跳过。或者不是吗? 它内部是如何工作的?
(为什么 case 语句中的变量不是局部变量? 指出 switch 语句内部是一组 jump
命令,但这仍然无法解释变量 someints
在哪里code> 已声明。)
Possible Duplicate:
Why are variables not local in case statements?
A variable defined in a scope block can't be used outside it. For example, the following code snippet is invalid:
{
int anothervar = 4;
}
{
System.out.println(anothervar);
}
But it looks like a case
block does not create apart scopes.
switch (mode) {
case ONE:
dosomething();
return;
case TWO:
int[] someints = new int[] { 2, 3, 5, 7 };
SomeObject obj = new SomeObject();
return;
case THREE:
someints = new int[] { 1, 4, 6, 8, 9 };
obj = new SomeObject();
return;
}
Why don't I have to declare someints
inside the case THREE
'block'?
Suppose mode = THREE
, then the declaration of variable someints
is never reached, because case TWO
, where someints
is declared, is skipped. Or isn't it? How does it work internally?
(The chosen answer in Why are variables not local in case statements? states that a switch statement is internally a set of jump
commands, but still that does not explain where the variable someints
is declared.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
局部变量的作用域位于块内,如 名称文档:
块被定义为具有封闭的大括号,正如 块和语句文档:
a local variable's scope is inside a block, as specified in the Names documentation:
A block is defined to have enclosing braces, as written as well in the Blocks and Statements documentation:
这就是为什么您有时会看到带有 : 的代码:
只有 { } 分隔块。
This is why you sometimes see code with :
Only the { } delimitates blocks.
声明的范围由
{ ... }
标记,您可能会在一秒钟内从一种情况中掉下来。虽然我承认这是一个坑。我相信在旧版本的 Java 中不允许在case
级别声明,并且必须添加额外的大括号:A scope of a declaration is marked by
{ ... }
and you may fall from one case in a second. I admit it is a hole though. I believe in an older version of Java one was not allowed to declare on thecase
level and had to add extra braces:除了其他回答者(正如他们所注意到的,{}之间的范围只有一个)之外,我还想注意到,这种做法:在一种情况下声明变量并在另一种情况下使用它,是肮脏的< /强>。稍后您可能希望删除或更改某些案例。如果是带有声明的,则必须更改另一个案例。这可能会导致错误。
彻底思考 - 为什么你的变量 someints 应该在这两种情况下?它真的是相同的东西还是两个不同的东西?
如果是一样的话:
最好在默认情况下声明公共变量并将其放在第一个。
或者将它们声明在 switch 块之外(这更糟糕)。或者将其放在定义常量的地方。
如果它们确实是不同的变量(可能只是在某些方面相似):
在每种情况下定义不同的变量。
顺便说一句:这是一个非常好的问题!
In addition to other answerers (as they noticed, the scope between {} is only one here) I would like to notice, that such practice: declaring of a variable in one case and using it in the other, is dirty. Later you can wish to remove or change some case. And if it will be the one with the declaration, you'll have to change another case. This could cause errors.
Think thoroughly - why your variable someints should be in both cases? Is it REALLY the same thing or two different?
If it is the same:
Better declare the common variables in the default case and put it as the first one.
Or declare them out of the switch block (it's worse). Or put it somewhere where you define constants.
If they really are different variables (may be only similar in some way):
define in each case a different variable.
BTW: It's a really nice question!
{ }
内的所有内容都在同一范围内。例如,如果您在情况 2 中取出return;
,则可以在情况 3 中使用该变量,前提是该变量已初始化。Everything inside the
{ }
is in the same scope. If you take out thereturn;
in case TWO for example, you can use the variable in case THREE, provided it's initialised.