Java 简明教程
Java - Variable Scopes
变量的作用域是指在给定的程序或函数中创建和访问它们的那个区域。变量作用域还指它的生命周期。
The variable’s scope refers to the region where they are created and accessed in a given program or function. The variable scope also refers to its lifetime.
在本教程中,我们将学习不同类型 Java variables 的范围。
In this tutorial, we will learn about the scopes of the different types of Java variables.
Scope of Java Instance Variables
在 class 内且在所有方法和块的外部声明的变量是一个实例变量。实例变量的一般范围在整个类中,静态方法除外。实例变量的生命周期是对象保留在内存中为止。
A variable which is declared inside a class and outside all the methods and blocks is an instance variable. The general scope of an instance variable is throughout the class except in static methods. The lifetime of an instance variable is until the object stays in memory.
Example: Scope of Java Instance Variables
在下面的示例中,我们定义了 Puppy 类中的实例变量 puppyAge,并使用了 setAge() 来修改它,使用了 getAge() 方法来获取它。此变量在 myPuppy 对象实例的生命期内可用。
In the example below, we define an instance variable puppyAge in Puppy class and using its setAge(), we’re modifying it and using getAge() method, we’re getting it. This variable is available till the lifetime of myPuppy object instance.
package com.tutorialspoint;
public class Puppy {
private int puppyAge;
public void setAge( int age ) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge( ) {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge( 2 );
System.out.println("Puppy Age :" + myPuppy.getAge() );
}
}
编译并运行程序。这将产生以下结果−
Compile and run the program. This will produce the following result −
Output
Puppy Age :2
Scope of Java Local Variables
在类内、在所有块外部声明并标记为静态的变量被称为类变量。类变量的一般作用域是在整个类的范围内,并且类变量的生命周期持续到程序结束或类加载到内存中。
A variable which is declared inside a class, outside all the blocks and is marked static is known as a class variable. The general scope of a class variable is throughout the class and the lifetime of a class variable is until the end of the program or as long as the class is loaded in memory.
Example: Scope of Java Local Variables
在下面的示例中,我们在 Puppy 类中定义了一个类变量 BREED。此变量在程序的整个生命周期内可用。由于其静态性质,我们可以直接使用类名来访问它,如下所示:
In the example below, we define a class variable BREED in Puppy class. This variable is available till the lifetime of program. Being static in nature, we can access it using class name directly as shown below:
package com.tutorialspoint;
public class Puppy {
private int puppyAge;
public static String BREED="Bulldog";
public void setAge( int age ) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge( ) {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge( 2 );
System.out.println("Puppy Age :" + myPuppy.getAge() );
// access the class variable
System.out.println("Breed :" + Puppy.BREED );
}
}
编译并运行程序。这将产生以下结果−
Compile and run the program. This will produce the following result −
Output
Puppy Age :2
Breed :Bulldog
Scope of Java Class (Static) Variables
所有其他不是实例变量和类变量的变量,包括方法中的参数,都将被视为局部变量。局部变量的作用域是在其被声明的块内,并且局部变量的生命周期持续到控制权离开其被声明的块时为止。
All other variables which are not instance and class variables are treated as local variables including the parameters in a method. Scope of a local variable is within the block in which it is declared and the lifetime of a local variable is until the control leaves the block in which it is declared.
Example: Scope of Java Class (Static) Variables
在下方的示例中,我们在 Puppy 类的 main() 方法中定义了两个本地变量。这些变量在声明所在的持续lifetime 方法/块中可用,可以如下所示访问:
In the example below, we define two local varables in main() method of Puppy class. These variables are availble till the lifetime of method/block in which these are declared and can be accessed as shown below:
package com.tutorialspoint;
public class Puppy {
private int puppyAge;
public static String BREED="Bulldog";
public void setAge( int age ) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge( ) {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge( 2 );
System.out.println("Puppy Age :" + myPuppy.getAge() );
// access the class variable
System.out.println("Breed :" + Puppy.BREED );
// local variables
int a = 10;
int b = 20;
int c = a + b;
System.out.println("c: " + c);
}
}
编译并运行程序。这将产生以下结果−
Compile and run the program. This will produce the following result −
Output
Puppy Age :2
Breed :Bulldog
c: 30
Important Points About Variables Scope
-
By default, a variable has default access. Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
-
A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
-
Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are − default − Visible to the package. No modifiers are needed.private − Visible to the class only.public − Visible to the world.protected − Visible to the package and all subclasses.