| Author |
static block
|
Tmmet Johnson
Ranch Hand
Joined: Nov 03, 2004
Posts: 56
|
|
Hi, I have the below code. Can anyone say me whether there is a possibility to have two instances of this singleton class? Is this the right way? Thanks in advance, public class Util { static private Util util = null; static { util = new Util(); }
|
 |
Scott Johnson
Ranch Hand
Joined: Aug 24, 2005
Posts: 518
|
|
The Util class is not a singleton. For starters, this class has a public (default) constructor so I can create as many instances as I want to. To avoid that problem, add a private constructor. Even if it was a singleton, there could be multiple instances created due to multiple JVMs or multiple class loaders within a single JVM. See this article for more info.
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
Even beyond the code, it is important to note that in a J2EE application, the various ways of packaging an application, and the way in which classloaders work, even a class coded according to what you would find in a typical Design Pattern Book, it is very possible that multiple instances of a singleton might be created. I love profiling a WebSphere application, and showing the developers all of the various instances of their 'singleton' that is running. I never get tired of watching a well intentioned developer blush. Cheers! -Cameron McKenzie
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
Tmmet Johnson
Ranch Hand
Joined: Nov 03, 2004
Posts: 56
|
|
Thanks!! So, even if I make the Util() constructor private, I might have one instance per JVM.And, this would be the same if I had the below. Please let me know whether I am correct or not. Thanks in advance, public class Util{ private Util(){} private static Util util = new Util(); public static Util getInstance(){ return util;} }
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
|
You will always have at least one instance per JVM -- JVMs will never share objects. But you also will have one per ClassLoader that loads the class. As K(C)ameron points out, this sort of situation comes up commonly in J2EE (EJB and Servlet) environments.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Tmmet Johnson
Ranch Hand
Joined: Nov 03, 2004
Posts: 56
|
|
Thanks for the replies. One clarification though.I understand that I will have one instance per JVM. I have deployed this application only in one JVM.No clustering or load balancing on my Websphere server. In my code, since, the class will loaded during application launch (as it is a static block), so, I will have only one instance right?Just want to make sure the approach that I used is same as public static Util util = new Util(); Thanks in advance,
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
|
Yes, the two are equivalent.
|
 |
 |
|
|
subject: static block
|
|
|