GeeCON Prague 2014*
The moose likes Java in General and the fly likes SingleTonpattern and Static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "SingleTonpattern and Static methods" Watch "SingleTonpattern and Static methods" New topic
Author

SingleTonpattern and Static methods

KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125
Hi guys,

Could anybody say the difference between singleton pattern and static methods in class.

When we have to use each one.


Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Singletons usually hold some state (variables) and you want to be sure there is only one copy in the JVM. Caches are good examples. I stuff data in there for use later because it's faster to use memory than a database or disk, and I want to be sure there is only one copy of the data.

Static methods often operate on the parameters only and don't use internal state. The Math class methods are good examples.

Sometimes we blend these things. For example static logger methods might use a singleton log formatter object.

Both Singleton and Static Method designs have some negatives. They often bind you closely to the implementing class, making it hard to subclass them or plug in different implementations. Look carefully for alternatives before you invest too much in these techniques.

Hope that helps! Wander down to the OO, UML, etc. forum for lots more conversation like this.
[ July 14, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
GeeCON Prague 2014
 
subject: SingleTonpattern and Static methods