开发者

Difference between accessing static instance variables using the keyword this and Class name

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);
    }开发者_如何学Go


    public static void main(String args[]){
        MyClass myclass = new myclass();
    }
}


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


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)


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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜