• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

static block

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the two are equivalent.
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic