I use a superclass which stores references to all instances of SuperClass
and various subclasses.
public class SuperClass{
private static final Map<Integer,? extends SuperClass> instances=new TreeMap<>();
private static final SuperClass inst1=new SuperClass(1,"1");
public final int ref; // exemplary comparable property
private final Object commonAttribute; //exemplary
protected SuperClass(Object o,int ref){
commonAttribute=o;
this.ref=ref;
instances.put(ref,this);
}
}
When I instantiate variables of the subclasses, I expect all instances to be added to instances
.
public class SubClass{
private final Object specificAttr;
private static final SubClass inst2=new SubClass(2,"2","whatever");
private final SubClass(Integer i,Object o1, Object o2){
super(i,o1);
this.specificAttribute=o2;
}
}
Confusingly the elements of instances
seem to depend on in which class main
method in. When I have the main
method in the Superclass
, only
inst1
is added, which is not desired in my case.
Is there any mechanism to customize which (static!) variables are initialised by jvm.