Java中的“范围"是什么? [英] What is 'scope' in Java?
问题描述
我刚开始编码.我想对同一个变量使用两次 switch 语句,我被告知要执行此操作,变量必须在范围内".
I just started coding. I want to use a switch statement twice for the same variable, and I was told that to do this the variable would have to be 'in scope'.
作为初学者,我不知道这意味着什么.那么在范围内是什么意思呢?而且,如果一个变量不在范围内,我该如何使它在范围内?
Being a beginner, I have no idea what that means. So what does being in scope mean? And, if a variable isn't in scope, how do I make it in scope?
推荐答案
局部变量1在范围内",如果代码可以访问它,如果不能,则超出范围.在 Java 中,变量的作用域是它们在其中声明的块 ({}
).所以:
A local variable1 is "in scope" if code can access it and out of scope if it can't. In Java, variables are scoped to the block ({}
) they're declared in. So:
void foo() {
int a = 42;
if (/*some condition*/) {
String q = "Life, the Universe, and Everything";
// 1. Both `a` and `q` are in scope here
System.out.println(a);
System.out.println(q);
if (/*another condition*/) {
// 2. Both `a` and `q` are in scope here, too
System.out.println(a);
System.out.println(q);
}
}
// 3. Only `a` is in scope here
System.out.println(a);
System.out.println(q); // ERROR, `q` is not in scope
}
注意上面的 (1)、(2) 和 (3):
Note (1), (2), and (3) above:
代码可以访问
q
,因为q
和代码在同一个块中声明;tt可以访问a
,因为它是在包含块中声明的.
The code can access
q
becauseq
is declared in the same block as the code; tt can accessa
because it's declared in the containing block.
代码可以访问q
,因为它是在包含块中声明的;它可以访问 a
因为它在下一个块中.
The code can access q
because it's declared in the containing block; it can access a
because it's in the next block out.
代码可以访问a
,但不能访问q
,因为q
没有在块或任何包含它的块(或其他一些东西).
The code can access a
, but not q
, because q
isn't declared in the block or any of the blocks (or a couple of other things) containing it.
当弄清楚什么是不合格的标识符时(如上面的 a
或 q
,而不是 this.foo 中的
foo
或 q.toLowerCase
中的 toLowerCase
,它们是 qualified) 是,Java 编译器会在每个地方查找,一个接一个,直到找到匹配项:
When figuring out what an unqualified identifier (like a
or q
above, as opposed to the foo
in this.foo
or the toLowerCase
in q.toLowerCase
, which are qualified) is, the Java compiler will look in each of these places, one after the other, until it finds a match:
- 对于最内层块中具有该名称的变量
- 对于下一个块中具有该名称的变量,依此类推
- 对于当前类中具有该名称的字段2或方法(通常:member)
- 对于来自已导入包的具有该名称的类
- 对于具有该名称的包
该列表中还有其他一些(我不打算与初学者讨论静态导入).
There are a few others for that list (I'm not going to get into static imports with a beginner).
范围还有很多,我建议您阅读一些教程和/或一本 Java 入门书以了解更多信息.
There's a lot more to scope, I suggest working through some tutorials and/or a beginning Java book for more.
1局部变量"与变量"——Java 语言规范 以比大多数人在普通语言中更通用的方式使用变量".当我在这个答案中说变量"时,我指的是 JLS 所说的local 变量".
1 "local variable" vs. "variable" - The Java Language Specification uses "variable" in a more general way than most people do in common speech. When I say "variable" in this answer, I mean what the JLS calls a "local variable".
2 "field" - JLS 在某些地方将字段称为变量"(在其他地方称为字段"),因此是上面的 (1).:-)
2 "field" - The JLS calls fields "variables" in some places (and "fields" in other places), hence (1) above. :-)
这篇关于Java中的“范围"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!