• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton pattern  RSS feed

 
sriram vara
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me which way is the best way to create a Singleton Object.

public class TestStatic {

static TestStatic test;

private TestStatic(){

test = new TestStatic();

}

public static TestStatic getInstance(){

return test;

}

}





OR







class iSpooler

{

//this is a prototype for a printer-spooler class

//such that only one instance can ever exist

static boolean instance_flag = false; //true if 1 instance

//the constructor is privatized-

//but need not have any content

private iSpooler() { }

//static Instance method returns one instance or null

static public iSpooler Instance()

{

if (! instance_flag)

{

instance_flag = true;

return new iSpooler(); //only callable from within

}

else

return null; //return no further instances

}

//-------------------------------------------

public void finalize()

{

instance_flag = false;

}

}

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how your first instance would work. Maybe it should look something like this:



This isn't thread-safe, but it should work.

John.
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither.

1) You need a private constructor, but you dont have to create an instance of the object in the constructor - that is the job of the constructor itself.
2) The getInstance() method should be synchronized. Atleast the block of code within the getInstance which is responsible for invoking the constructor should be synchronized for thread safety.
3) If by any chance your class or a superclass has implemented the Cloneable interface, you'll have to override the clone() method [just throw a CloneNotSupportedException]

Dawn.
 
sriram vara
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You . When i ran the code i realized my mistake.I was thinking when you call the getInstance method the private constructor will be invoked and you will have the object .But looks like we have to instantiate it in the getInstance(). There is not much difference b/w what john has posted here and what i asked the
OR way of doing singleton(they use a flag to control instantiation). I understood it .Also will make it thread safe as per dawn's comments.

Thanks
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another often used approach is the following:
This code is thread safe, and has only one drawback - you always create the singleton instance, even if you never need it.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest using an Enum to provide Singleton behavior.

Enum's classes are better than the approaches using private constructors . It is more concise and provides the guarantee against multiple instantiation, even when dealing with serialization and/or reflection. (See Item 3 of Effective Java)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!