简体   繁体   中英

Static variables in an Abstract Class java

If I have an abstract class that has variables of the form

protected static final int blah;
protected static final int blah2;

And i have two classes that extend this abstract class, and set these variables to "static final int" values from a constants file in their constructors, will they clobber eachother's values? If I want to do such a thing, what would you recommend I do?

So, for example, if i have

impl1 class:

public impl1 extends absClass{
    public impl1(){
        this.blah = CONSTANTS.impl1_blah;
        this.blah2 = CONSTANTS.impl1_blah2;
    }
}

impl2 class:

public impl2 extends absClass{
    public impl2(){
        this.blah = CONSTANTS.impl2_blah;
        this.blah2 = CONSTANTS.impl2_blah2;
    }
}

Is this allowed? If not, what should I do?

this.blah = CONSTANTS.impl2_blah;
this.blah2 = CONSTANTS.impl2_blah;

this allowed?

This isn't allowed, since your blah variables are declared as final . You must initialize them during class initialization, either in their declaration or in a static initializer block.

Furthermore, these variables are static , and so accessing them using this won't work: the variables belong to the class and not an instance .

If not, what should I do?

Use non-final variables in the superclass, or use specific constants in the subclasses.

如果扩展该抽象类的类应该为这些变量赋予自己的值,那么您应该考虑使用几个受保护的抽象方法。

static variables can't be overridden. Those will be associated with the classes where you have defined them.

static final variables must be initialized when the class that declares them is initialized. This is before any instances of the class (or any subclass) are created. Your code won't compile without some sort of initialization for blah and blah2 —either an initialization expression:

protected static final int blah = 42;
protected static final int blah2 = 1;

or in a static initializer block:

protected static final int blah;
protected static final int blah2;
static {
    blah = 42;
    blah2 = 1;
}

In either case, subclasses have no say in what blah and blah2 get assigned.

It seems from your example code that you want constants that can vary on a per-instance basis. It doesn't make sense for them to be static. You can do something like this:

public AbsClass {
    protected final int blah;
    protected final int blah2;
    protected AbsClass(int blah, int blah2) {
        this.blah = blah;
        this.blah2 = blah2;
    }
    . . .
}

public class Impl1 extends AbsClass {
    public Impl1() {
        super(CONSTANTS.impl1_blah, CONSTANTS.impl1_blah2);
    }
}

public class Impl2 extends AbsClass {
    public Impl1() {
        super(CONSTANTS.impl2_blah, CONSTANTS.impl2_blah2);
    }
}

Polymorphism in Java don't work with attributes. Use some protected abstract getMethods() instead.

First of all this is final+static which means nothing to do with OOP, If you make them not static then It makes sense to talk about OOP on it. If you do not do them as private and access them using getters/setters then you are breaking encapsulation.

And you are making it final, means it can be initialized only once. You will get exception when you try to change the value of final attr.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM