• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How can I write ....

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I write a class, so that only one object can be created for the class?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Thiru Mu:
How can I write a class, so that only one object can be created for the class?


You write a constructor which has private access modifier and create an instance in a static method and return the object reference.
class A
{
private A()
{}
static A getInstance()
{
A a = new A();
return a;
}
}
this way only one object will be created for class A.
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nousheed Mohammed:

You write a constructor which has private access modifier and create an instance in a static method and return the object reference.
class A
{
private A()
{}
static A getInstance()
{
A a = new A();
return a;
}
}
this way only one object will be created for class A.



From the way you wrote the getInstance() method, can't someone just call your method more than once? And get more than once instance of the class?

Henry
 
Nousheed Mohammed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


From the way you wrote the getInstance() method, can't someone just call your method more than once? And get more than once instance of the class?

Henry


The object will be created once in static method and if someone calls this static method more than once, object reference will be returned... it is called singleton class
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Actually, to implement a singleton, you'd synchronize the method and also first check if the object exists before creating it. See below:

public static synchronized SingletonObject getInstance()
{
if ( a == null)
A a = new A();
return a;
}

Happy Studying
 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nousheed Mohammed:

The object will be created once in static method and if someone calls this static method more than once, object reference will be returned... it is called singleton class



Take a look at the code again. The variable "a" is a local variable. A new object will be returned every time -- nowhere in the code is there a check for "more than once".

Henry
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nousheed Mohammed:

The object will be created once in static method and if someone calls this static method more than once, object reference will be returned... it is called singleton class




Howdy Nousheed Mohammed,

Welcome to JavaRanch

Thanks for the contributions.

Henry was intended to point out a flaw in the code (even when you say, a Singleton class). The main intention is to restrict the total number of objects being created for the class is one and only one under any circumstances.

The way you have written the getInstance() method would fit in for just a demonstration but is not foolproof. Just to give you a clue, you may have to "protect" method in such a way that no two accesses to the method happen simultaneously (at the same time).

So, your method needs some improvements!
 
Annette Sovereign
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick correction, the object in the snippet I wrote earlier would be declared private and not in the method.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!