The specific use when I thought about this problem is as follows, but it is much more generalized.
I have my own JFrame class, which also serves as an ActionListener for its components. So my constructor looks something like this:
private JButton myButton; public MyCustomFrame() { super(); myButton.addActionListener(this);
My question is, how does this really work behind the scenes? If the constructor is what “creates” the object referenced by this , how can I use this until the constructor returns? The code compiles and works just fine (as far as I can tell), so the object should already "exist" in a way, but I'm worried that this could cause unforeseen problems. Is there any danger when passing a "partially constructed" link to addActionListener() (or just executing any logic with it at all)? Or is there some kind of disguising magic that keeps me safe?
For example, what about things that do not have default values and should be provided by the constructor? If I am declared private final String SOME_VALUE; , I understand that this should be null by default, but the object should not be fully formed until a constant is provided with a value inside the constructor. So will the link, although it is final, possibly change the meaning?
source share