• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton class  RSS feed

 
Gopinath Reddy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • 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.
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!