Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block

 
Tmmet Johnson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tmmet Johnson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tmmet Johnson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the two are equivalent.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic