Wednesday, January 14, 2009

Couplig the final circular references

Another initializatin problem, which precludes using "final" modifier for the fields is a circular reference: one object refers another, which refers the first object. For example, the two ends of a pipe are constructed sequentially and, thus, the counterpart, which was constructed first, cannot be marked "final". Yet, the references are final - the coupled parts constitute an indivisable solid object.

This is akin to the previous post because the following code produces "variable hBar cannot be initialized":
final JScrollBar hBar;
final JViewport viewPort = new JViewport() {
 {
  this.addChangeListener(new ChangeListener() {
   public void stateChanged(ChangeEvent e) {
    hBar.setValues(hBar.getValue(), getExtentSize().width,
      0, getViewSize().width);
   }
  });
 }
};
hBar = new JScrollBar(Adjustable.HORIZONTAL) {
 {
  getModel().addChangeListener(new ChangeListener() {

   public void stateChanged(ChangeEvent e) {
    frame.setTitle(getModel().getValue() + "");
   }

  });
 }
};

No comments: