Java 开关盒中变量的作用域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3894119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Variable's scope in a switch case
提问by Philippe Carriere
I think I don't understand how the scope works in a switch case.
我想我不明白示波器在开关盒中是如何工作的。
Can someone explain to me why the first code doesn't compile but the second does ?
有人可以向我解释为什么第一个代码不能编译但第二个可以吗?
Code 1 :
代码 1:
int key = 2;
switch (key) {
case 1:
String str = "1";
return str;
case 2:
String str = "2"; // duplicate declaration of "str" according to Eclipse.
return str;
}
Code 2 :
代码 2:
int key = 2;
if (key == 1) {
String str = "1";
return str;
} else if (key == 2) {
String str = "2";
return str;
}
How come the scope of the variable "str" is not contained within Case 1 ?
为什么变量“str”的范围不包含在案例 1 中?
If I skip the declaration of case 1 the variable "str" is never declared...
如果我跳过 case 1 的声明,则永远不会声明变量“str”...
采纳答案by Richard Cook
I'll repeat what others have said: the scope of the variables in each case
clause corresponds to the whole switch
statement. You can, however, create further nested scopes with braces as follows:
我将重复其他人所说的:每个case
子句中变量的范围对应于整个switch
语句。但是,您可以使用大括号创建更多嵌套范围,如下所示:
int key = 2;
switch (key) {
case 1: {
String str = "1";
return str;
}
case 2: {
String str = "2";
return str;
}
}
The resulting code will now compile successfully since the variable named str
in each case
clause is in its own scope.
由于str
每个case
子句中命名的变量在其自己的范围内,因此生成的代码现在将成功编译。
回答by Drew Wills
The scope of the variable is the whole switch
statement -- all cases and default, if included.
变量的范围是整个switch
语句——所有情况和默认值(如果包含)。
Here are some other options...
这里有一些其他的选择......
Option 1:
选项1:
int key = 2;
switch (key) {
case 1:
return "1";
case 2:
return "2";
}
Option 2:
选项 2:
int key = 2;
String str = null;
switch (key) {
case 1:
str = "1";
return str;
case 2:
str = "2";
return str;
}
回答by Stan Kurilin
Several cases can be executed in one switch statement. So..
在一个 switch 语句中可以执行多种情况。所以..
回答by JB.
The scope of a variable exists between the braces of the switch and if statements. In example Code 1 the switch braces enclose both declarations of the variables which will cause the compiler to error as the name to variable binding will have already been made.
变量的作用域存在于 switch 和 if 语句的大括号之间。在示例代码 1 中,开关大括号包含变量的两个声明,这将导致编译器错误,因为名称到变量的绑定已经完成。
In the other example it is ok because both variables are declared within their own braces (scope).
在另一个示例中,这没问题,因为两个变量都在它们自己的大括号(范围)内声明。
回答by leonbloy
You seem to be assuming that each case
is a block with its own local scope, as if/else blocks. It's not.
您似乎假设每个case
都是一个具有自己局部范围的块,就像 if/else 块一样。它不是。
It's important to correct this conceptual mistake, because otherwise you'll end falling in the frequent trap of forgetting the break
inside the case
纠正这个概念上的错误很重要,否则你会经常陷入忘记break
内部结构的陷阱。case
回答by Emil
In the first case the scope of the String declaration is within the switch statement therefore it is shown as duplicate while in the second the string is enclosed within curly braces which limits the scope within the if/else conditions,therefore it is not an error in the second case.
在第一种情况下,字符串声明的范围在 switch 语句内,因此它显示为重复,而在第二种情况下,字符串括在花括号内,这将范围限制在 if/else 条件内,因此它不是错误第二种情况。
回答by Sachin Kumar
I think it's a valid question, and scope assumption for case statment is unavoidable. Adjusting ourselves to it because java writer has made this not correct.
我认为这是一个有效的问题,案例陈述的范围假设是不可避免的。调整我们自己,因为 Java writer 使这不正确。
e.g. if statement by default take first line in its scope than what's wrong with cases where the end of case is explicitly closed by break statement. Hence the declaration in case 1: should not be available in case 2 and it has parallel scope but not nested.
例如,默认情况下 if 语句在其范围内采用第一行,而不是在 case 结束由 break 语句显式关闭的情况下有什么问题。因此,case 1: 中的声明在 case 2 中不可用,并且它具有并行作用域但不是嵌套的。