通过实例引用访问的静态成员(使用 'this' 关键字)
Static member accessed via instance reference (using 'this' keyword)
public class RoundCapGraph extends View {
static private int strokeWidth = 20;
public void setStrokeWidth(int strokeWidth){
this.strokeWidth = strokeWidth;
//warning : static member 'com.example.ud.RoundCapGraph.strokeWidth' accessed via instance reference
}
}
在 android 工作室中,我正在尝试使用 setStrokeWidth 设置 strokeWidth。
但我收到警告
通过实例引用访问的静态成员'com.example.ud.RoundCapGraph.strokeWidth'
问题:'this'关键字是否创建新实例并通过新实例访问变量?
已编辑:我真的不需要将 strokeWidth 变量设置为静态,但我想了解为什么使用 'this' 关键字会产生特定的警告
正确,静态成员属于 class 而不是实例。
this
关键字不创建新实例,但this.
通常用于访问实例变量。
因此,当编译器发现您试图通过 this.
访问 static
变量时,它会假设您可能犯了一个错误(即您的意图是访问一个实例变量), 所以它会发出警告。
访问 static
变量的更好方法是:
RoundCapGraph.strokeWidth = strokeWidth;
编辑:您正在实例方法中设置 static
变量。这很好地表明编译器警告您有关访问 static
变量就好像它是一个实例变量一样是正确的。
您应该通过 static
方法设置 static
变量,并通过实例方法设置实例变量。
当您使用对象的实例访问静态成员时,该实例会被 Class 替换。即 this.strokeWidth
将替换为 RoundCapGraph.strokeWidth
由于实例替换,不会有NullPointerException
我在 Java Specification:第 15 章第 11 节:字段访问表达式中找到了对此的引用。
Example 15.11.1-2. Receiver Variable Is Irrelevant For static Field Access
The following program demonstrates that a null reference may be used to access a class (static) variable without causing an exception
public class RoundCapGraph extends View {
static private int strokeWidth = 20;
public void setStrokeWidth(int strokeWidth){
RoundCapGraph roundCapGraph = null;
roundCapGraph.strokeWidth = strokeWidth; // NullPointerException?
//warning : static member 'com.example.ud.RoundCapGraph.strokeWidth' accessed via instance reference
}
}
public class RoundCapGraph extends View {
static private int strokeWidth = 20;
public void setStrokeWidth(int strokeWidth){
this.strokeWidth = strokeWidth;
//warning : static member 'com.example.ud.RoundCapGraph.strokeWidth' accessed via instance reference
}
}
在 android 工作室中,我正在尝试使用 setStrokeWidth 设置 strokeWidth。
但我收到警告
通过实例引用访问的静态成员'com.example.ud.RoundCapGraph.strokeWidth'
问题:'this'关键字是否创建新实例并通过新实例访问变量?
已编辑:我真的不需要将 strokeWidth 变量设置为静态,但我想了解为什么使用 'this' 关键字会产生特定的警告
正确,静态成员属于 class 而不是实例。
this
关键字不创建新实例,但this.
通常用于访问实例变量。
因此,当编译器发现您试图通过 this.
访问 static
变量时,它会假设您可能犯了一个错误(即您的意图是访问一个实例变量), 所以它会发出警告。
访问 static
变量的更好方法是:
RoundCapGraph.strokeWidth = strokeWidth;
编辑:您正在实例方法中设置 static
变量。这很好地表明编译器警告您有关访问 static
变量就好像它是一个实例变量一样是正确的。
您应该通过 static
方法设置 static
变量,并通过实例方法设置实例变量。
当您使用对象的实例访问静态成员时,该实例会被 Class 替换。即 this.strokeWidth
将替换为 RoundCapGraph.strokeWidth
由于实例替换,不会有NullPointerException
我在 Java Specification:第 15 章第 11 节:字段访问表达式中找到了对此的引用。
Example 15.11.1-2. Receiver Variable Is Irrelevant For static Field Access
The following program demonstrates that a null reference may be used to access a class (static) variable without causing an exception
public class RoundCapGraph extends View {
static private int strokeWidth = 20;
public void setStrokeWidth(int strokeWidth){
RoundCapGraph roundCapGraph = null;
roundCapGraph.strokeWidth = strokeWidth; // NullPointerException?
//warning : static member 'com.example.ud.RoundCapGraph.strokeWidth' accessed via instance reference
}
}