开发者

How to access static variables from within object in Java?

From within the class, how do I access a static variable explicitly? (Is the best practice to access static variable explicitly eg. using static.staticVar)

The below works

class Something {
    protected static _var1;

    public vo开发者_如何学运维id somefunc() {
        return _var1; 
    }
}

But how do i specify it explicitly? (Is specifying explicitly recommended?)


I suggest you do what is the simplest and clearest code you can. It a judgement call most of the time and if you are working with people you can ask them.

I avoid using a mutable static field if at all possible. However, in this case you can write

protected static Type s_var1; // a convention for static mutable fields

public Type somefunc() {
    return Something.s_var1; // Has to be a static field.
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜