使用关键字 this 和类名访问静态实例变量的区别

发布于 2024-10-08 16:57:44 字数 548 浏览 4 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

德意的啸 2024-10-15 16:57:44

它们都是等价的。
但是,使用 this 访问静态成员会产生误导,应不惜一切代价避免。

They are both equivalent.
However, accessing static members using this is misleading and should be avoided at all costs.

天荒地未老 2024-10-15 16:57:44

没有区别。

有些人认为静态字段或方法应该通过类名访问,而不是通过 this 或实例来突出显示它是静态的代码>.例如,Eclipse 有一个配置设置来标记有关通过实例引用访问静态资源的警告。

我的偏好,按顺序:

  • 在类本身中,我将仅引用不带限定符的字段

  • 使用类名称

  • 仅当您认为字段/方法的静态性质可能会改变并且它的客户端是静态的时才使用 this 或实例类不应该依赖(在这种情况下,我会考虑将其设为实例方法,以确保如何访问它)

No difference.

Some feel that a static field or method should be accessed through the Class name, rather than through this or an instance, to highlight that it is a static. 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)

寻找一个思念的角度 2024-10-15 16:57:44

在这种情况下没有区别,我认为它被编译为相同的字节码(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.

恋竹姑娘 2024-10-15 16:57:44

来自 Java 教程:了解实例和类成员

类变量被引用
类名本身,如

Bicycle.numberOfBicycles

这清楚地表明他们是
类变量。

注意:您还可以使用对象引用来引用静态字段,例如

myBike.numberOfBicycles

但不鼓励这样做,因为它并没有明确表明它们是
类变量。

From Java Tutorial: Understanding Instance and Class Members:

Class variables are referenced by the
class name itself, as in

Bicycle.numberOfBicycles

This makes it clear that they are
class variables.

Note: You can also refer to static fields with an object reference like

myBike.numberOfBicycles

but this is discouraged because it does not make it clear that they are
class variables.

眼中杀气 2024-10-15 16:57:44

您班级的所有实例共享相同的密码;密码属于,而不是每个实例,这就是静态的含义。

因此,尽管您可以从任何实例访问它,但不鼓励这种使用,因为它表明密码是特定于实例的。

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.

只是我以为 2024-10-15 16:57:44

请记住,静态变量在 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.

一江春梦 2024-10-15 16:57:44

嗯,静态变量不是实例变量。因此,静态实例变量不是一个有效术语。

Well, static variables are not instance variables. Therefore , static instance variable is not a valid term.

束缚m 2024-10-15 16:57:44

您不需要使用任何一个。

因为您有一个内部类,您可以直接访问静态变量,而无需显式引用外部类或 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文