Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

JPanel this reference escape in constructor.

Thread view: 
Daniel Pitts - 26 Mar 2007 03:35 GMT
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?
Chris Uppal - 26 Mar 2007 08:38 GMT
> The JPanel constructor calls updateUI, which passes the "this"
> reference to UIManager.getUI().
[quoted text clipped - 3 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.