class Sub {
static int y;
public static void foo() {
this.y = 10;
}
}
I understand that this
represents the object invoking the method and that static methods are not bound to any object. But in the above mentioned case, the variable y is also static.
我理解这表示调用方法的对象,并且静态方法不绑定到任何对象。但是在上面的例子中,变量y也是静态的。
If we can invoke static method on class object, why can't we allow static methods to set the static variables of the class.
如果我们可以在类对象上调用静态方法,为什么我们不能允许静态方法来设置类的静态变量。
What is the purpose of this additional constraint?
这个附加约束的目的是什么?
8 个解决方案
#1
71
Because this
refers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!). Just use
因为这是指对象实例。静态方法的调用中没有对象实例。当然,您可以访问静态字段(只有静态字段!)只使用
class Sub {
static int y;
public static void foo() {
y = 10;
}
}
If you want to make sure you get the static field y
and not some local variable with the same name, use the class name to specify:
如果要确保获得静态字段y,而不是使用相同名称的局部变量,请使用类名指定:
class Sub {
static int y;
public static void foo(int y) {
Sub.y = y;
}
}
#2
5
this
is referring to this instance of the object Sub
. As the method is static
, there is not an instance of Sub
.
这指的是对象子的这个实例。由于方法是静态的,所以没有子的实例。
#3
4
The main reason why we can not use "this" in static method context:-
我们不能在静态方法上下文中使用“this”的主要原因是:-。
this :- "this" means current class OBJECT , so its clear that "this" only come in the picture once we intended to create an Object of that class.
这个:-“this”表示当前的类对象,因此很明显,“this”只出现在我们打算创建该类的对象的时候。
static method:- there is no need to create an object in order to use static method. means "instance" or object creation doesn't any sense with "static" as per Java rule.
静态方法:-不需要创建对象来使用静态方法。表示“实例”或对象创建与“静态”的Java规则没有任何意义。
So There would be contradiction,if we use both together(static and this) . That is the reason we can not use "this" in static method.
如果我们同时使用(静态和这个),就会有矛盾。这就是我们不能在静态方法中使用“this”的原因。
#4
2
To make your code work write it like this:
让你的代码像这样写:
class Sub {
static int y;
public static void foo() {
Sub.y = 10;
}
}
You can set static fields in static methods, but you don't have access to this
in static method because this
represents the current instance of the object, and in a static method you have no instance.
您可以在静态方法中设置静态字段,但是在静态方法中无法访问它,因为这表示对象的当前实例,在静态方法中您没有实例。
#5
2
This means "this" object but there isn't one. In your case you can use the class name as @tibtof suggests.
这意味着“这个”对象,但没有一个。在您的示例中,可以使用@tibtof建议的类名。
#6
0
Keyword "this" refers to the object that you are operation with. In your case this inside any non-static methods or constructor (if you have one and and if you use "this" inside that), then "this" refers to that particular instance of the class Sub.So it is applicable only when the object is created. But anything in the static context of a class, you can use without even creating object for that as it is resolved during the class loading. "this" resolved only when object is created ( you can even say dynamically for which object). So "this" make sense in static context. Hope it helps. God bless.
关键字“this”是指您正在操作的对象。在您的情况下,在任何非静态方法或构造函数中(如果您有一个,并且如果您在其中使用“this”),那么“this”是指该类的特定实例,所以只有在创建对象时才适用。但是,在类的静态上下文中,您可以在不创建对象的情况下使用它,因为它在类加载期间得到了解析。“这个”只有在创建对象时才会解析(您甚至可以动态地对该对象说)。在静态环境中,这是有意义的。希望它可以帮助。上帝保佑。
#7
0
"this" keyword is only applicable where an instance of an object is created. And in static method no instance is created because static method belongs to class area.
“this”关键字只适用于创建对象的实例。在静态方法中,没有创建实例,因为静态方法属于类区域。
#8
0
I agree with all other people who replied before me. Let me try it in different way to answer this:
我同意所有在我面前回答的人。让我用不同的方法来回答这个问题
I guess, instance method/non-static method belongs to instance of a class (meaning sooner or later we need object ref to access it) so this keyword make sense inside instance block or method. But static keyword to any member of a class is interpreted as direct asset to class which a object if existed then has access to it. So in static context it's not sure that object is existing somewhere. That's why using this inside static area is not allowed in java.
我猜,实例方法/非静态方法属于类的实例(意味着迟早我们需要对象ref来访问它),所以这个关键字在实例块或方法中是有意义的。但是,对于类中的任何成员,静态关键字被解释为类的直接资产,如果存在,则对象可以访问它。在静态环境中,不确定对象是否存在。这就是为什么在java中不允许使用这个内部静态区域的原因。
#1
71
Because this
refers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!). Just use
因为这是指对象实例。静态方法的调用中没有对象实例。当然,您可以访问静态字段(只有静态字段!)只使用
class Sub {
static int y;
public static void foo() {
y = 10;
}
}
If you want to make sure you get the static field y
and not some local variable with the same name, use the class name to specify:
如果要确保获得静态字段y,而不是使用相同名称的局部变量,请使用类名指定:
class Sub {
static int y;
public static void foo(int y) {
Sub.y = y;
}
}
#2
5
this
is referring to this instance of the object Sub
. As the method is static
, there is not an instance of Sub
.
这指的是对象子的这个实例。由于方法是静态的,所以没有子的实例。
#3
4
The main reason why we can not use "this" in static method context:-
我们不能在静态方法上下文中使用“this”的主要原因是:-。
this :- "this" means current class OBJECT , so its clear that "this" only come in the picture once we intended to create an Object of that class.
这个:-“this”表示当前的类对象,因此很明显,“this”只出现在我们打算创建该类的对象的时候。
static method:- there is no need to create an object in order to use static method. means "instance" or object creation doesn't any sense with "static" as per Java rule.
静态方法:-不需要创建对象来使用静态方法。表示“实例”或对象创建与“静态”的Java规则没有任何意义。
So There would be contradiction,if we use both together(static and this) . That is the reason we can not use "this" in static method.
如果我们同时使用(静态和这个),就会有矛盾。这就是我们不能在静态方法中使用“this”的原因。
#4
2
To make your code work write it like this:
让你的代码像这样写:
class Sub {
static int y;
public static void foo() {
Sub.y = 10;
}
}
You can set static fields in static methods, but you don't have access to this
in static method because this
represents the current instance of the object, and in a static method you have no instance.
您可以在静态方法中设置静态字段,但是在静态方法中无法访问它,因为这表示对象的当前实例,在静态方法中您没有实例。
#5
2
This means "this" object but there isn't one. In your case you can use the class name as @tibtof suggests.
这意味着“这个”对象,但没有一个。在您的示例中,可以使用@tibtof建议的类名。
#6
0
Keyword "this" refers to the object that you are operation with. In your case this inside any non-static methods or constructor (if you have one and and if you use "this" inside that), then "this" refers to that particular instance of the class Sub.So it is applicable only when the object is created. But anything in the static context of a class, you can use without even creating object for that as it is resolved during the class loading. "this" resolved only when object is created ( you can even say dynamically for which object). So "this" make sense in static context. Hope it helps. God bless.
关键字“this”是指您正在操作的对象。在您的情况下,在任何非静态方法或构造函数中(如果您有一个,并且如果您在其中使用“this”),那么“this”是指该类的特定实例,所以只有在创建对象时才适用。但是,在类的静态上下文中,您可以在不创建对象的情况下使用它,因为它在类加载期间得到了解析。“这个”只有在创建对象时才会解析(您甚至可以动态地对该对象说)。在静态环境中,这是有意义的。希望它可以帮助。上帝保佑。
#7
0
"this" keyword is only applicable where an instance of an object is created. And in static method no instance is created because static method belongs to class area.
“this”关键字只适用于创建对象的实例。在静态方法中,没有创建实例,因为静态方法属于类区域。
#8
0
I agree with all other people who replied before me. Let me try it in different way to answer this:
我同意所有在我面前回答的人。让我用不同的方法来回答这个问题
I guess, instance method/non-static method belongs to instance of a class (meaning sooner or later we need object ref to access it) so this keyword make sense inside instance block or method. But static keyword to any member of a class is interpreted as direct asset to class which a object if existed then has access to it. So in static context it's not sure that object is existing somewhere. That's why using this inside static area is not allowed in java.
我猜,实例方法/非静态方法属于类的实例(意味着迟早我们需要对象ref来访问它),所以这个关键字在实例块或方法中是有意义的。但是,对于类中的任何成员,静态关键字被解释为类的直接资产,如果存在,则对象可以访问它。在静态环境中,不确定对象是否存在。这就是为什么在java中不允许使用这个内部静态区域的原因。