I have an abstract class that is somewhat like a View from Android. I create a lot of classes that extend it to draw different stuff. Now i would like all those classes to share the same Paints so that colors match and so on.
My ideas would be
pass some context or windowmetrics to every single constructor, but that feels silly as i only need it once.
i could add a static method init() to the abstract class, but i try to avoid public static methods.
create a subclass with the single purpose to set the static members of the superclass and also null them at the end, something like a remote control to the superclass' static stuff.
im just not sure what risks there are or if there are even simpler ways to do it.
Edit: to init the static members i need a context (for those not familiar with android) and that context needs to be passed to that class, so no init in static blocks etc
Or just add a static block and initialize them there:
public abstract class Foo {
public static final int DEFAULT_PAINTS_SIZE = 5;
public static Paint [] paints;
static {
paints = new Paints[DEFAULT_PAINTS_SIZE];
// initialize the values somehow.
}
}
You can initialize them directly:
public class MyClass {
private static MyStatic myStaticObject = new MyStatic();
}
or in a static initializer block:
public class MyClass {
private static MyStatic myStaticObject;
static {
//something = stuff
myStaticObject = new MyStatic(something);
//more stuff
}
}
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.