2

For thread-safety reasons it is argumented:

Do not allow the this reference to escape during construction.

But is this always an issue and should be avoided by using newInstance() methods? Inside my model class I have a TableModel which should be instantiated, within the model class, but which also requires a reference to the model class:

public class MainModel {

   TableModel tableMode;

   public MainModel() {
      tableModel = new MyTableModel(this);
   }
}

If the constructor does not use this right away is it then safe or should it be avoided in any means?

1
  • 1
    I have seen it the official (Sun/Oracle) Swing tutorials. Commented Jun 30, 2011 at 14:25

3 Answers 3

6

If nothing in the MyTableModel is going to do anything in other threads etc - or copy the variable to some other shared data, such as a static variable - then it's safe.

Of course, if MyTableModel starts calling methods on the MainModel reference within its constructor, then it'll be calling them on a not-completely-initialized-yet object, which can cause issues - but that's not really threading related.

I blogged a bit more on this a while ago.

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

Comments

4

No, I don't think it's always a problem. In my opinion a good class is designed such that the constructor limits the activity it does on its dependents, limiting its behaviour only to initialization. If that's the case it would be very surprising for this to leak to another thread simply because you leaked it to another constructor.

The only time you are not permitted at all to leak a reference to this is before the super contructor has been called. In other words, you can't pass an argument to the super constructor that has a dependency on this, be it due to you calling an instance method or constructing something using this.

I think a better question might be why does MyTableModel need to see an instance of MainModel? Often bi-directional visibility is a sign of some harmful coupling.

4 Comments

Peters: Thanks for pointing out the problem in your second statement. Maybe you can help: The table model needs the main model as the main model provides the data for it. The main model needs a reference to the table model, so the observer.notify method can fire a data change to it.
@platzhirsch: I'm not exactly sure how you're dividing up the model, but if MainModel is the data/model behind your TableModel, then it should not be the one that is creating the TableModel. With separation of concerns, it's best if your model does not know anything about presentation, and that's exactly what you're doing by having your main model create a TableModel. Push that creation into your presentation code.
Peters: Uff, I was told TableModel is part of the model component in MVC.
@platzhirsch: It is, but you need to recognize that there are different levels of models and different levels of applying MVC. You don't want your data to have to know that it's going to be rendered in a table, when in another presentation it might be rendered in a list, etc. So when you create your presentation component, that's when you should create your MyTableModel and inject it into your presentation component as that specific component's model. Alternatively you might consider putting that in the controller.
-2

Best pratice is not about correcting bugs, but patterns which are least likely to introduce bugs, be confusing or difficult to maintain. I have seen some increadibly difficult code which works just fine.

e.g. I remember one class called c which was written entirely on one line to save space (no line breaks) and only used single character variables/fields and had one to two character methods. This wasn't deliberately obfuscated, the developer thought this was the most efficient. The class worked fine as long as you didn't need to understand it of change it.

2 Comments

This response does not address the question at all.
@Will The question is But is this always an issue ... ?. My answer is that its should be avoided but it doesn't mean it won't work, or that it will be an issue.

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.