When we usually want to create a collection consisting of POJOs like List<MyObject> = new ArrayList<>();
we usually create public class MyObject
on its own.
But what if we create a private static MyObject
inner class in the parent class in which the collection is declared?
Here's an example:
public class MyClass {
public static void main(String args[]) {
List<MyObj> objList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
MyObj obj = new MyObj();
obj.name = "Odel B Jr.";
obj.age = "2";
objList.add(obj);
}
System.out.println(objList);
}
private static class MyObj {
private String name;
private String age;
@Override
public String toString() {
return "MyObj [name=" + name + ", age=" + age + "]";
}
}
}
Notice that this creates 10 new
static Objects.
My thing is, which is better? 10 new static objects or 10 new regular java objects?
I'm trying to approach this from JVM's point of view - i.e. with a focus on performance. Not really about design/accessibility of objects according to Java OOP principles.