Assume I have a Warehouse class. That Warehouse has a TradeDesk. That TradeDesk computes available items to sell based on an instance variable set in Warehouse. A Warehouse's constructor instantiates a TradeDesk, but because TradeDesk needs an instance variable from the incompletely initialized Warehouse, we have a problem. Short of passing the instance value through multiple constructors (which I'd rather avoid; note that the below example is significantly simplified), how do I solve this? Thanks!
public class Warehouse {
TradingDesk td;
public int val;
public Warehouse() {
val = 3;
td = new TradingDesk(this);
}
// New class
public class TradingDesk {
Warehouse associatedWh;
int val;
public TradingDesk(Warehouse wh) {
associatedWh = wh;
val = associatedWh.val;
}
}
}