1
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();

2 Answers 2

3

xTable and 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 mtOrders will not be a MyType, but a MyTypeRef, which will have a MyType field. Then, do initialize it to a new MyTypeRef, and use the loop to set the MyType objects in all the refs. Since both the static variables and the xTable entries will point on the same MyTypeRef, setting the MyType field 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, I'm not crazy about this solution, because it adds an extra wrapper to every variable just to bypass some initialization issue. But it's your code...
My initialization really much complex, than i showed. I thinking, that simplier add new var to array, than copy-paste a lot lines of code.
2

Java doesn't have a way of initializing a reference variable other than assigning to it. You can perhaps get close to what you want with a map:

public static final String myOrders = "mtOrders";
// etc.

public final static Map<String, MyType> xTable;

static {
    HashMap<String, MyType> table = new HashMap<String, MyType>();
    String[] keys = { myOrders, . . . }
    for (int i = 0; i < keys.length; ++i) {
        table.put(keys[i], new MyType());
    }
    xTable = Collections.unmodifiableMap(table);
}

Then you can get at individual elements by:

MyType orders = xTable.get(myOrders);

If this doesn't meet your needs, you'll just have to assign each field individually.

1 Comment

Thank you for answer. I'll do as I suggested by eran.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.