Java 静态方法可以访问非静态实例变量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38263533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can static method access non-static instance variable?
提问by SERich
So my understanding was that you can't use static method to access non-static variables, but I came across following code.
所以我的理解是你不能使用静态方法来访问非静态变量,但我遇到了以下代码。
class Laptop {
String memory = "1GB";
}
class Workshop {
public static void main(String args[]) {
Laptop life = new Laptop();
repair(life);
System.out.println(life.memory);
}
public static void repair(Laptop laptop) {
laptop.memory = "2GB";
}
}
Which compiles without errors.
编译没有错误。
So isn't
所以不是
public static void repair(Laptop laptop) {
laptop.memory = "2GB";
}
accessing String memory defined in class Laptop, which is non-static instance variable?
访问类笔记本电脑中定义的字符串内存,这是非静态实例变量?
Since the code compiles without any error, I'm assuming I'm not understanding something here. Can someone please tell me what I'm not understanding?
由于代码编译没有任何错误,我假设我不理解这里的某些东西。有人可以告诉我我不明白的地方吗?
采纳答案by Thomas
A static method can access non-static methods and fields of any instance it knows of. However, it cannot access anything non-static if it doesn't know which instance to operate on.
静态方法可以访问它知道的任何实例的非静态方法和字段。但是,如果它不知道要操作哪个实例,则它无法访问任何非静态内容。
I think you're mistaking by examples like this that don't work:
我认为您误会了像这样不起作用的例子:
class Test {
int x;
public static doSthStatically() {
x = 0; //doesn't work!
}
}
Here the static method doesn't know which instance of Test
it should access. In contrast, if it were a non-static method it would know that x
refers to this.x
(the this
is implicit here) but this
doesn't exist in a static context.
这里的静态方法不知道Test
它应该访问哪个实例。相反,如果它是一个非静态方法,它会知道x
指的是this.x
(this
这里是隐式的)但this
不存在于静态上下文中。
If, however, you provide access to an instance even a static method can access x
.
但是,如果您提供对实例的访问,即使是静态方法也可以访问x
.
Example:
例子:
class Test {
int x;
static Test globalInstance = new Test();
public static doSthStatically( Test paramInstance ) {
paramInstance.x = 0; //a specific instance to Test is passed as a parameter
globalInstance.x = 0; //globalInstance is a static reference to a specific instance of Test
Test localInstance = new Test();
localInstance.x = 0; //a specific local instance is used
}
}
回答by Bhargav Kumar R
You can access only with object reference
.
您只能使用 访问object reference
。
Instance variables defined at class level, have to be qualified with object name if you are using in a static context. But it does not not mean that you cannot access at all.
如果在静态上下文中使用,则在类级别定义的实例变量必须使用对象名称进行限定。但这并不意味着您根本无法访问。
回答by Arun Raaj
Static methods cannot modify their value. You can get their current value by accessing them with the reference of current class.
静态方法不能修改它们的值。您可以通过使用当前类的引用访问它们来获取它们的当前值。
回答by VRadhe
try this code
试试这个代码
public static void repair() {
Laptop laptop =new Laptop();
laptop.memory="2GB";
}