1

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?

6 Answers 6

3
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, i made an edit, i added '2's' at the end of them, is it okay now?
1

if classes extending that abstract class are supposed to give their own values for those variables then you should consider a couple of protected abstract methods instead.

Comments

0

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

8 Comments

So basically, impl2's blah variable will not equal impl1's blah variable right? And why can't I use the this keyword?
Yes you are correct. I mis-understood this.blah in your question, if this.blah is impl1 variable, then no issues.
Correction: final variables can't be overwritten. You can overwrite static variables just fine, but that changes them for anything accessing them anywhere else in this process.
@Supuhstar: Are you sure? Check my sample below. As per your theory, when I assign 50 to temp in Superclass and print it from MySubClass it should give me 50, but printing 30.
public abstract class MySuperClass { public static int temp = 10; } public class MySubClass extends MySuperClass{ public static int temp = 30; } public class MyTestClasss { public static void main(String[] args) { MySuperClass.temp =50; System.out.println(MySubClass.temp); } }
|
0

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);
    }
}

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.