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 / GUI / December 2004

Tip: Looking for answers? Try searching our database.

Why two instantiations?

Thread view: 
spam_to_dev_null@yahoo.com - 14 Dec 2004 16:22 GMT
Gurus:

Two questions:

1. Regarding the following code, why is updateUI called twice?
ie, once implicitly by B's constructor at line 8 and once explicitly
at line 9.

2. Why is a second instance of Object B created?  That's the only
explanation that makes sense given alreadyUpdatedUI's behavior.
ie, if this flag is declared to be static at line 5 then the second
call to updateUI shows that alreadyUpdatedUI is true and, hence,
only one dumpStack gets printed.  Note also that it doesn't matter
if updateUI is declared synchronized or not.

Thank you,

import javax.swing.*;

class B extends JButton
{
boolean alreadyUpdatedUI = false;  // line 5

public B ()
{                               // line 8
updateUI ();                    // line 9
}

public void updateUI ()
{
System.out.println ("alreadyUpdateUI: " + alreadyUpdatedUI);
if (alreadyUpdatedUI)
return;
alreadyUpdatedUI = true;
Thread.currentThread ().dumpStack ();
}
}

public class A
{
public static void main (String args[])
{
B newB = new B ();
}
}

% java A
alreadyUpdateUI: false
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1071)
at B.updateUI(A.java:18)
at javax.swing.AbstractButton.init(AbstractButton.java:1930)
at javax.swing.JButton.<init>(JButton.java:109)
at javax.swing.JButton.<init>(JButton.java:64)
at B.<init>(A.java:8)
at A.main(A.java:26)
alreadyUpdateUI: false
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1071)
at B.updateUI(A.java:18)
at B.<init>(A.java:9)
at A.main(A.java:26)

% java -version
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)

% uname -a
SunOS sunbox 5.8 Generic_117000-03 sun4u sparc SUNW,Sun-Blade-100
Boudewijn Dijkstra - 14 Dec 2004 18:55 GMT
> Gurus:
>
[quoted text clipped - 10 lines]
> only one dumpStack gets printed.  Note also that it doesn't matter
> if updateUI is declared synchronized or not.

You haven't proven that there are two instances of B.  You can do this by
printing out System.identityHashCode(this) from inside updateUI().


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.