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
))是,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:
- 对于最里面的块中具有该名称的变量
- 对于下一个屏蔽区中具有该名称的变量,依此类推
- 对于在当前类中具有该名称的 field 2 或方法(通常为 member )
- 对于已导入包中具有该名称的类
- 对于具有该名称的包裹
- For a variable with that name in the innermost block
- For a variable with that name in the next block out, and so on
- For a field2 or method (generally: member) with that name in the current class
- For a class with that name from a package that's been imported
- For a package with that name
该列表还有其他一些(我不会和初学者一起进入静态导入).
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 字段"-JLS在某些地方将字段称为变量"(在其他地方将字段称为字段"),因此在上面称为(1). :-)
2 "field" - The JLS calls fields "variables" in some places (and "fields" in other places), hence (1) above. :-)
这篇关于Java中的“作用域"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!