Jeankowkow
Jeankowkow

Reputation: 834

Initialize several static attributes of several classes from one place (Java)

I have several class (Sub1, Sub2, Sub3) that inherit all from an abstract class (Abs). All the Sub classes have several properties (size, delay, etc.) that are the same for all instances (like a static attribute). I would like to regroup all these subclasses attributes (and accessors) to avoid duplication of code and initialize it from only one place. Is it possible?

Here is a sample of code that show what I am forced to do to obtain the desired behavior:

public abstract class Abs {
    protected static final int DEFALUT_SIZE = 1000;
    protected static final int DEFALUT_DELAY = 1000;
}

class Sub1 extends Abs {
    private static int size;
    private static int delay;

    public Sub1() {
        size = DEFALUT_SIZE;
        delay = DEFALUT_DELAY;
    }

    public void setSize(int size) { this.size = size; }
    public int getSize() { return size; }
    public void setDelay(int delay) { this.delay = delay; }
    public int getDelay() { return delay; }
}

class Sub2 extends Abs {
    private static int size;
    private static int delay;

    public Sub1() {
        size = DEFALUT_SIZE;
        delay = DEFALUT_DELAY;
    }

    public void setSize(int size) { this.size = size; }
    public int getSize() { return size; }
    public void setDelay(int delay) { this.delay = delay; }
    public int getDelay() { return delay; }
}

class Sub3 extends Abs {
    /* Same code here */
}

I would like to do the following:

Sub1 sub1 = new Sub1();

int size = Sub1.getSize(); // size == 1000

Sub1.setSize(500);
Sub2.setSize(700);

size = sub1.getSize(); // size == 500
size = Sub1.getSize(); // size == 500
size = Sub2.getSize(); // size == 700

With this solution, I am forced to rewrite all the same code in each subclasses.

I learned that the static attributes were not inherited, so impossible to do so from the abstract class...

Upvotes: 0

Views: 54

Answers (1)

Software Engineer
Software Engineer

Reputation: 16100

It's generally a good idea to assume that, in all cases except constants, the keyword static is evil and should be avoided at all costs.

If you removed the static keyword from in front of size and delay, made them protected, and moved them into the abstract Abs class along with the initialisation code to set their initial values and the accessors and mutators (getters and setters), then you'd be able to remove their declarations and initialisation from the subclasses.

Your code should look something like this:

public abstract class Abs {
private static final int DEFALUT_SIZE = 1000;
private static final int DEFALUT_DELAY = 1000;

private int size = DEFAULT_SIZE;
private int delay = DEFAULT_DELAY;

public void setSize(int size) { this.size = size; }
public int getSize() { return size; }
public void setDelay(int delay) { this.delay = delay; }
public int getDelay() { return delay; }
}

Then anything that extends the Abs class will have these properties.

To share these properties between objects you'll have to do something a little different though, but there are many options. You can have a common class that holds the shared values that is injected into each instance at creation, or you can use some sort of eventing/observer pattern. However, this is quite an odd thing to want in the first place. Typically we'd have some sort of central object representing this information and just pass that around as needed -- you wouldn't typically have setters and getters on subclasses to implement this. What you have is akin to a service managing global properties.

Upvotes: 1

Related Questions