• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singleton class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Can any one explain whats the advantage of using singleton classes?
Is there any relationship between static methods and singleton.

Thanks in advance
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Reddy,


Is there any relationship between static methods and singleton.


You need at least one getInstance() static method. Not sure if this is the answer you�re looking for, but with most of the J2EE application developers are just afraid of synchronizing the getInstance() method and they prefer to use static instance variables (which by the way is a great approach specially with singletons). In practice though as well as in theory it is absolutely fine (in my opinion) to implement a singleton synchronizing the getInstance() method, even with J2EE applications:



But people just prefer to stay on the safety side and follow J2EE specs and completely avoid to word synchronize within their code.
Regards.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Reddy ,

Singleton class comes into picture when there is common data which is frequently share/used by varoius module in ur application.
And creation of commonly shared data for every request is expensive for the application viz. Remote Lookup of Session Bean or loading and reading one property from property file.
There should only one static method which will return the instance of that singleton class.Because on-buddy can construct the object of ur Singleton class {coz constructor of ur singleton is private }.
And as per me any static method is not suppose to manipulate Member Valiable realted data , coz if something goes wrong during the processing, then other request for that member will get wrong/unexpected result.

U always create static method for commond peice of code which take some input and process those input and return the result, e.g sorting.

Hope that this make more clear when to use static method and when to use Singleton class.

Sunil Dixit
 
reply
    Bookmark Topic Watch Topic
  • New Topic