Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I force class to be a static

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,
How do I force class to be a static.
Meaning I want to create only one instance of that class.
Thanks in advance
siva
 
Stephen Pride
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to use a Singleton class (if I understand you correctly).
For example ...

This will guarantee only one instance of the class. Note the private constructor and private/static object.
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a good idea to synchronize the getInstance() method of a singleton, just on the off chance that two threads try to simultaneously call it for the first time. Without the synchronization, there is a slight chance that you could get two instances.
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I am new user this forum and
finding very usful. Thanks to all the great folks here.
thanks
siva
x38020
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See http://c2.com/cgi/wiki?SingletonPattern for a discussion on the subject. Specifically, be very carefull about overusing this pattern...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic