I would like to know if is it expensive in terms of resource usage to declare a static field to keep track of all istances of that class in a class that I need to istantiate thousand of times. I usually make another class like the example below, but I wonder if I could make everything more compact without wasting resources.
1
public class Object {
public static List<Object> objects = new ArrayList<>();
public Object() {
objects.add(this);
}
//My code
}
2
public class Object {
public Object() {
ObjectManager.addObject(this);
}
//My code
}
public class ObjectManager {
private static List<Object> objects = new ArrayList<>();
public static void addObject(Object obj) {
objects.add(obj);
}
}
What's the difference between these 2 methods?