使用关键字 this 和类名访问静态实例变量的区别
我有以下 java 类。当我在构造函数中调用登录方法时,我使用类名访问静态实例变量username
,并使用关键字this访问静态实例变量
。我的问题是这两种方法有什么区别?在什么情况下应该使用哪一个?password
public class MyClass {
private Main main;
private static String username = "mylogindetails";
private static String password = "mypassword";
public MyClass(){
this.main = new Main();
this.main.login(MyClass.username, this.password);
}
public static void main(String args[]){
MyClass myclass = new myclass();
}
}
I have got the following java class. When I am calling the login method in my constructor, I access the static instance variable username
using the class name, and the static instance variable password
using the keyword this
. My question is what is the difference between the two approaches? Which one should be used in what situation?
public class MyClass {
private Main main;
private static String username = "mylogindetails";
private static String password = "mypassword";
public MyClass(){
this.main = new Main();
this.main.login(MyClass.username, this.password);
}
public static void main(String args[]){
MyClass myclass = new myclass();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它们都是等价的。
但是,使用
this
访问静态成员会产生误导,应不惜一切代价避免。They are both equivalent.
However, accessing static members using
this
is misleading and should be avoided at all costs.没有区别。
有些人认为静态字段或方法应该通过类名访问,而不是通过 this 或实例来突出显示它是静态的代码>.例如,Eclipse 有一个配置设置来标记有关通过实例引用访问静态资源的警告。
我的偏好,按顺序:
在类本身中,我将仅引用不带限定符的字段
使用类名称
仅当您认为字段/方法的静态性质可能会改变并且它的客户端是静态的时才使用
this
或实例类不应该依赖(在这种情况下,我会考虑将其设为实例方法,以确保如何访问它)No difference.
Some feel that a
static
field or method should be accessed through the Class name, rather than throughthis
or an instance, to highlight that it is astatic
. Eclipse, for example, has a config setting to flag a warning about a static resource being accessed through an instance reference.My preferences, in order:
within the class itself, I would just reference the field without a qualifier
use the Class name
use
this
or an instance only if you feel the static nature of the field/method may change and it is a design detail that it is static that the clients of the class should not depend on (in which case, I would think about making it an instance method anyway to ensure how it is accessed)在这种情况下没有区别,我认为它被编译为相同的字节码(GETSTATIC)。
但
MyClass.username
是首选,因为它反映了该字段的性质。There is no difference in this case, and I think it's compiled to the same bytecode (GETSTATIC).
But
MyClass.username
is preferred, because it reflects the nature of the field.来自 Java 教程:了解实例和类成员:
From Java Tutorial: Understanding Instance and Class Members:
您班级的所有实例共享相同的密码;密码属于类,而不是每个实例,这就是静态的含义。
因此,尽管您可以从任何实例访问它,但不鼓励这种使用,因为它表明密码是特定于实例的。
All instances of your class share the same password; password belongs to the class, not each instance, that's what static means.
So although you can access it from any instance, such usage is discouraged as it suggests the password is instance specific.
请记住,静态变量在 MyClass 的所有实例之间共享,因此使用 this 来引用它们会产生误导。实际上,这只是风格上的差异,但您不应该养成用它来引用它的习惯。
Remember that static variables are shared across all instances of your MyClass so using this to refer to them is misleading. Really it's just a style difference but you should not get in the habit of referring to it with this.
嗯,静态变量不是实例变量。因此,静态实例变量不是一个有效术语。
Well, static variables are not instance variables. Therefore , static instance variable is not a valid term.
您不需要使用任何一个。
因为您有一个内部类,您可以直接访问静态变量,而无需显式引用外部类或 this
如果使用静态字段令人困惑,您可以可以将它们全部大写以区分它们。通常,这是为充当常量的最终静态字段保留的。
You don't need to use either.
Because you've got an inner class you can directly access the static variable without an explicit reference to the outer class or to this
If using static fields is confusing, you can make them all caps to distinguish them. Often this is reserved for final static fields acting as constants though.