Java中的变量范围
变量的范围是程序中可以访问变量的部分。与 C/C++ 一样,在Java中,所有标识符都是词法(或静态)范围的,变量的 ie 范围可以在编译时确定并且独立于函数调用堆栈。
Java程序以类的形式组织。每个类都是某个包的一部分。 Java范围规则可以涵盖在以下类别中。
成员变量(类级别范围)
这些变量必须在类内(任何函数外)声明。它们可以在课堂的任何地方直接访问。我们来看一个例子:
public class Test
{
// All variables defined directly inside a class
// are member variables
int a;
private String b;
void method1() {....}
int method2() {....}
char c;
}
- 我们可以在类中的任何位置声明类变量,但在方法之外。
- 指定成员变量的访问权限不会影响它们在类中的范围。
- 成员变量可以通过以下规则在类外部访问
Modifier Package Subclass World
public Yes Yes Yes
protected Yes Yes No
Default (no
modifier) Yes No No
private No No No
局部变量(方法级别范围)
在方法内声明的变量具有方法级范围,不能在方法外访问。
public class Test
{
void method1()
{
// Local variable (Method level scope)
int x;
}
}
注意:方法执行结束后局部变量不存在。
这是方法范围的另一个示例,除了这次变量作为参数传递给方法:
class Test
{
private int x;
public void setX(int x)
{
this.x = x;
}
}
上面的代码使用这个关键字来区分局部变量和类变量。
作为练习,预测以下Java程序的输出。
Java
public class Test
{
static int x = 11;
private int y = 33;
public void method1(int x)
{
Test t = new Test();
this.x = 22;
y = 44;
System.out.println("Test.x: " + Test.x);
System.out.println("t.x: " + t.x);
System.out.println("t.y: " + t.y);
System.out.println("y: " + y);
}
public static void main(String args[])
{
Test t = new Test();
t.method1(5);
}
}
Java
public class Test
{
public static void main(String args[])
{
{
// The variable x has scope within
// brackets
int x = 10;
System.out.println(x);
}
// Uncommenting below line would produce
// error since variable x is out of scope.
// System.out.println(x);
}
}
Java
class Test
{
public static void main(String args[])
{
for (int x = 0; x < 4; x++)
{
System.out.println(x);
}
// Will produce error
System.out.println(x);
}
}
Java
// Above program after correcting the error
class Test
{
public static void main(String args[])
{
int x;
for (x = 0; x < 4; x++)
{
System.out.println(x);
}
System.out.println(x);
}
}
Java
class Test
{
public static void main(String args[])
{
int a = 5;
for (int a = 0; a < 5; a++)
{
System.out.println(a);
}
}
}
Java
class Test
{
public static void main(String args[])
{
{
int x = 5;
{
int x = 10;
System.out.println(x);
}
}
}
}
Java
class Test {
public static void main(String args[])
{
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
int i = 20;
System.out.println(i);
}
}
输出:
Test.x: 22
t.x: 22
t.y: 33
y: 44
循环变量(块范围)
在方法中的一对括号“{”和“}”内声明的变量仅在括号内具有作用域。
Java
public class Test
{
public static void main(String args[])
{
{
// The variable x has scope within
// brackets
int x = 10;
System.out.println(x);
}
// Uncommenting below line would produce
// error since variable x is out of scope.
// System.out.println(x);
}
}
输出:
10
作为另一个示例,请考虑以下带有 for 循环的程序。
Java
class Test
{
public static void main(String args[])
{
for (int x = 0; x < 4; x++)
{
System.out.println(x);
}
// Will produce error
System.out.println(x);
}
}
输出:
11: error: cannot find symbol
System.out.println(x);
上面的正确做法是,
Java
// Above program after correcting the error
class Test
{
public static void main(String args[])
{
int x;
for (x = 0; x < 4; x++)
{
System.out.println(x);
}
System.out.println(x);
}
}
输出:
0
1
2
3
4
让我们看一下循环范围的棘手示例。预测以下程序的输出。如果您是普通的 C/C++ 程序员,您可能会感到惊讶。
Java
class Test
{
public static void main(String args[])
{
int a = 5;
for (int a = 0; a < 5; a++)
{
System.out.println(a);
}
}
}
输出 :
6: error: variable a is already defined in method go(int)
for (int a = 0; a < 5; a++)
^
1 error
注意:- 在 C++ 中,它将运行。但是在Java中这是一个错误,因为在Java中,内循环和外循环的变量名必须不同。
C++ 中的类似程序可以工作。看到这个。
作为练习,预测以下Java程序的输出。
Java
class Test
{
public static void main(String args[])
{
{
int x = 5;
{
int x = 10;
System.out.println(x);
}
}
}
}
Q. 根据以上知识,判断下面的代码是否会运行。
Java
class Test {
public static void main(String args[])
{
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
int i = 20;
System.out.println(i);
}
}
输出 :
1
2
3
4
5
6
7
8
9
10
20
是的,它会运行!
仔细看程序,内循环在外循环变量被声明之前就终止了。所以内循环变量先被销毁,然后再创建同名的新变量。
关于Java中变量作用域的一些要点:
- 通常,一组大括号 { } 定义了一个范围。
- 在Java中,我们通常可以访问一个变量,只要它与我们正在编写的代码在同一组括号内定义,或者在定义变量的大括号内的任何大括号内定义。
- 所有成员方法都可以使用在任何方法之外的类中定义的任何变量。
- 当方法与成员具有相同的局部变量时,可以使用“this”关键字来引用当前类变量。
- 对于循环终止后要读取的变量,它必须在循环体之前声明。