Abstract class with final uninitialized field

java, oop

Solution

I would make the field final and force the constructors to pass the value up:

public abstract class Test {
  private final ArrayList<Object> objects;

  protected ArrayList<Object> getObjects() {
    return objects;
  }

  protected Test(ArrayList<Object> objects) {
    this.objects = objects;
  }
}

public class TestSubA extends Test {

  public TestSubA() {
    super(new ArrayList<Object>(20));
    // Other stuff
  }
}

public class TestSubB extends Test {

  public TestSubB() {
    super(new ArrayList<Object>(100));
    // Other stuff
  }
}

Problem

I was wondering if the below code makes any sense, since the compiler warns that "the blank final field objects may not have been initialized". Is there a better way of doing this? ``` public abstract Test { protected final ArrayList<Object> objects; } public TestSubA extends Test { public TestSubA() { objects = new ArrayList<Objects>(20); // Other stuff } } public TestSubB extends Test { public TestSubB() { objects = new ArrayList<Objects>(100); // Other stuff } } ```

Original source