public static MyType mtOrders;
public static MyType mtCustomers;
public static MyType mtItems;
public static MyType mtGroups;
public static MyType mtDelieverAddresses;
public static MyType mtVendors;
public static MyType mtOrderItems;
public static MyType mtPrims;
public final static MyType[] xTable = {mtCustomers, mtGroups, mtItems, mtOrders,
mtDelieverAddresses, mtVendors, mtOrderItems, mtPrims};
for (int i = 0; i < xTables.length; i++) {
xTable[i] = new MyType();
}
After executing xTable‘s elements are initialized, but mtOrders… mtPrims are null!
I understand why this is so, but I can not think how I initialize the objects in the loop.
I do not want to do this:
mtOrders = new MyType();
mtCustomers = new MyType();
...
mtPrims = new MyType();
xTableand that series of static variables are different references, that might point on the same object, but cannot be used to modify each other. Namely, they can both be used to modify the same object, but having one point on an object will not make the other point on that object as well.I don’t quite understand your actual goal. Best I can offer is to use some intermediate class they will all point to. So
mtOrderswill not be aMyType, but aMyTypeRef, which will have aMyTypefield. Then, do initialize it to a newMyTypeRef, and use the loop to set theMyTypeobjects in all the refs. Since both the static variables and thexTableentries will point on the sameMyTypeRef, setting theMyTypefield on one will affect the other as well.IMO, using that loop instead of initializing each separate variable looks like tighter code, but it doesn’t work. Just write a few more lines of code, so it’s not as pretty but actually works.