JPanel this reference escape in constructor.

D

Daniel Pitts

The JPanel constructor calls updateUI, which passes the "this"
reference to UIManager.getUI().

I thought that it was incorrect to allow your "this" reference to
escape your class during construction. Is this an oversight in the
implementation of JPanel, or did I misunderstand the assertion about
escape?

Is it only that you have to ensure that your "this" reference doesn't
escape the constructing thread? If that is true, then that means that
constructing a JPanel must be done on the EDT, to ensure proper
functioning. Am I missing something here?
 
C

Chris Uppal

Daniel said:
The JPanel constructor calls updateUI, which passes the "this"
reference to UIManager.getUI().

I thought that it was incorrect to allow your "this" reference to
escape your class during construction. Is this an oversight in the
implementation of JPanel, or did I misunderstand the assertion about
escape?

Depends on what you mean by "incorrect". It is often a mistake to allow "this"
to escape from an incompletely initialised object (but it isn't always a
mistake -- sometimes the design calls for it explicitly, and sometimes there's
no other way reasonable of designing things).

OTOH, it isn't an /error/ (in the sense of something that Java will reject) to
do so. It /is/ an error to [try to] allow a reference to "this" to escape
before the superclass constructor has completed; and Java will reject code
which tries to do that. But once you are into the code of your own
constructor, the language feels that the object is old enough to look after
itself, and any mistakes thereafter are your responsibility ;-)
Is it only that you have to ensure that your "this" reference doesn't
escape the constructing thread? If that is true, then that means that
constructing a JPanel must be done on the EDT, to ensure proper
functioning. Am I missing something here?

Not escaping the current thread is a weak condition for correctness, not strong
enough always to avoid problems, but weaker than forbidding references to
incomplete objects altogether.

In this case, I presume that the JPanel constructor has been coded so that the
reference doesn't escape until the object itself is in a safe enough state (but
I'm still unconvinced by the quality of such code -- the JPanel implementation
doesn't know what further requirements subclasses may have which it is just
ignoring).

But there is a secondary issue: as you say, it looks as if JPanels should only
be constructed on the EDT -- not so much because they allow "this" to escape
early, but because the constructor invokes AWT code at all.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top