java 中局部变量和 class 变量的行为
Behaviour of local and class variables in java
我是 Java 编程语言的新手。
我熟悉 C 和 C++,但无法理解以下程序的行为。
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);
}
}
正确输出:
Test.x: 22
t.x: 22
t.y: 33
y: 44
预期输出:
Test.x: 22
t.x: 22
t.y: 44 // As variable y is modified inside the function.
y: 44
即使将行从 y = 44;
更改为 this.y = 44;
也没有给出预期的输出。
问题是您指的不是实际创建的对象。您正在从其他具有新变量的实例中引用变量。
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);
如果你仔细观察第一行 Test t = new Test();
您没有在 y 分配给 44 的特定实例上调用 method1。因此您看到的是顶级值。
重命名实例会更清楚。而不是总是 t
。
这就是造成混乱的原因,而且,您在内部调用 method1()
可能会导致无限循环。
在 method1
你有两个对象 t
和 this
(当前对象)和行
y = 44; // equivalent to this.y = 44
正在设置当前对象的值,所以
this.y == 44; or y == 44;
t.y == 33;
你要明白的一点是y = 44 or this.y = 44不修改t.y,如果你想修改t.y的值你可以做这个:
t.y = 44;
静态变量和非静态变量的基本区别
class Student {
private int id;
private String name;
static String collegeName;
}
对于 Students non-static
的每个对象,属性 id 和 name 将以其初始值(0 和 null)加载到内存中,每个对象的 id 和 name 可以不同。但是 collegeName 只会加载一次,即加载 class 执行时。因此 Student
的每个对象都将具有相同的大学名称。这就是static
.
的意思
访问静态和非静态变量
class Student {
private int id;
private String name;
static String collegeName;
public static void main(String[] args) {
String s1 = Student.collgeName;
String s2 = collgeName;
Student student = new Student();
String s3 = student.name;
int id = student.id;
}
}
静态变量可以直接使用它们的名称或使用 class 名称的帮助来访问。当有一个静态全局变量和一个局部变量时,静态变量应该与class名称一起使用
public static void main(String[] args) {
String s1 = Student.collgeName;
String collgeName = "foo";
String output = collgeName;
}
此处 output
的值为“foo”。局部变量总是比全局静态变量具有更高的优先级,这就是为什么 String output = s1;
会给出输出值 null
.
在静态块中non-static
必须借助引用变量来访问变量(我们必须创建一个对象)。 Main 方法是静态的,这就是为什么我们必须创建 Student
的对象来访问 id
和 name
的值,否则会给出编译时错误。
关于非静态块的盲法
每个非静态块都将使用默认的 this
关键字,表示在使用 class 级别(静态和非静态)变量时调用块的当前引用.示例 java 代码
class Student {
private int id;
private String name;
static String collegeName;
void setData() {
id = 1;
name = "foo";
collegeName = "FooCollege";
}
public static void main(String[] args) {
Student student = new Student();
student.setData();
}
}
这是编译相同代码以获取 class 文件时发生的情况
class Student {
private int id;
private String name;
static String collegeName;
void setData() {
this.id = 1;
this.name = "foo";
this.collegeName = "FooCollege"; // which will be again as Student.collegeName
}
public static void main(String[] args) {
Student student = new Student();
student.setData();
}
}
这里this
表示main方法中的引用变量student
。 表示调用块的引用变量。
来到这个问题,main 方法创建了一个 Test
对象并调用了它的引用 method1()
。所以在 method1
this 里面只不过是在 main 方法中创建的引用变量 t
而 t
是该方法的局部引用变量。现在让我们以 class 文件格式
重新编写代码
public void method1(int x) {
Test t = new Test();
this.x = 22; // or Test.x = 22;
y = 44; // or this.y = 44;
/*
Test object inside method1 and main method are in two different locations.
When we write this.y = 44; the y inside the main method object will be changed and not the one created inside method1.
*/
System.out.println("Test.x: " + Test.x);
System.out.println("t.x: " + t.x);
System.out.println("t.y: " + t.y); // means the y inside the object created inside method1
System.out.println("y: " + y); // means the y inside the object created inside main method
}
我是 Java 编程语言的新手。
我熟悉 C 和 C++,但无法理解以下程序的行为。
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);
}
}
正确输出:
Test.x: 22
t.x: 22
t.y: 33
y: 44
预期输出:
Test.x: 22
t.x: 22
t.y: 44 // As variable y is modified inside the function.
y: 44
即使将行从 y = 44;
更改为 this.y = 44;
也没有给出预期的输出。
问题是您指的不是实际创建的对象。您正在从其他具有新变量的实例中引用变量。
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);
如果你仔细观察第一行 Test t = new Test();
您没有在 y 分配给 44 的特定实例上调用 method1。因此您看到的是顶级值。
重命名实例会更清楚。而不是总是 t
。
这就是造成混乱的原因,而且,您在内部调用 method1()
可能会导致无限循环。
在 method1
你有两个对象 t
和 this
(当前对象)和行
y = 44; // equivalent to this.y = 44
正在设置当前对象的值,所以
this.y == 44; or y == 44;
t.y == 33;
你要明白的一点是y = 44 or this.y = 44不修改t.y,如果你想修改t.y的值你可以做这个: t.y = 44;
静态变量和非静态变量的基本区别
class Student {
private int id;
private String name;
static String collegeName;
}
对于 Students non-static
的每个对象,属性 id 和 name 将以其初始值(0 和 null)加载到内存中,每个对象的 id 和 name 可以不同。但是 collegeName 只会加载一次,即加载 class 执行时。因此 Student
的每个对象都将具有相同的大学名称。这就是static
.
访问静态和非静态变量
class Student {
private int id;
private String name;
static String collegeName;
public static void main(String[] args) {
String s1 = Student.collgeName;
String s2 = collgeName;
Student student = new Student();
String s3 = student.name;
int id = student.id;
}
}
静态变量可以直接使用它们的名称或使用 class 名称的帮助来访问。当有一个静态全局变量和一个局部变量时,静态变量应该与class名称一起使用
public static void main(String[] args) {
String s1 = Student.collgeName;
String collgeName = "foo";
String output = collgeName;
}
此处 output
的值为“foo”。局部变量总是比全局静态变量具有更高的优先级,这就是为什么 String output = s1;
会给出输出值 null
.
在静态块中non-static
必须借助引用变量来访问变量(我们必须创建一个对象)。 Main 方法是静态的,这就是为什么我们必须创建 Student
的对象来访问 id
和 name
的值,否则会给出编译时错误。
关于非静态块的盲法
每个非静态块都将使用默认的 this
关键字,表示在使用 class 级别(静态和非静态)变量时调用块的当前引用.示例 java 代码
class Student {
private int id;
private String name;
static String collegeName;
void setData() {
id = 1;
name = "foo";
collegeName = "FooCollege";
}
public static void main(String[] args) {
Student student = new Student();
student.setData();
}
}
这是编译相同代码以获取 class 文件时发生的情况
class Student {
private int id;
private String name;
static String collegeName;
void setData() {
this.id = 1;
this.name = "foo";
this.collegeName = "FooCollege"; // which will be again as Student.collegeName
}
public static void main(String[] args) {
Student student = new Student();
student.setData();
}
}
这里this
表示main方法中的引用变量student
。 表示调用块的引用变量。
来到这个问题,main 方法创建了一个 Test
对象并调用了它的引用 method1()
。所以在 method1
this 里面只不过是在 main 方法中创建的引用变量 t
而 t
是该方法的局部引用变量。现在让我们以 class 文件格式
public void method1(int x) {
Test t = new Test();
this.x = 22; // or Test.x = 22;
y = 44; // or this.y = 44;
/*
Test object inside method1 and main method are in two different locations.
When we write this.y = 44; the y inside the main method object will be changed and not the one created inside method1.
*/
System.out.println("Test.x: " + Test.x);
System.out.println("t.x: " + t.x);
System.out.println("t.y: " + t.y); // means the y inside the object created inside method1
System.out.println("y: " + y); // means the y inside the object created inside main method
}