It's not a secret anymore!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes difference between the singleton pattern class and static class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "difference between the singleton pattern class and static class" Watch "difference between the singleton pattern class and static class" New topic
Author

difference between the singleton pattern class and static class

Anna Madhusudhanan Natanagopalan
Greenhorn

Joined: Apr 05, 2004
Posts: 25
Hi,
What is the difference between the singleton pattern class and static class?
singleton allows only one instatnce and the static class also.
what is the differnece ?

Can any one explain me.

Thanks and regards
Anna Madhusudhanan
Praveen Yadav
Greenhorn

Joined: Apr 11, 2004
Posts: 5
Hi
1) Methods that are static cannot be used to implement an interface. Hence we lose a great deal of the power of Java's interface entity.
2) All references to the singleton must be hard-coded with the name of the class. Hence there is no meaningful way to override the static methods. In other words, all static methods may as well be final.


---------------------<br />Praveen <br /> SCJP2.0
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
The main difference is extensibility. Because static methods are tied to a particular class, there's no way to use virtual method calls. (This is the reason that, as the last poster correctly stated, you can't implement an interface with static methods--by definition, all calls to interface methods must be delegated to an implementing class, and hence are virtual. Incidentally, the only non-virtual methods in Java besides static are those that are marked final.)

The purpose of a design pattern like Singleton is to address a problem that occurs in many different situations generally, and that's not usually possible unless you provide for extensibility. Since static methods cannot be overridden, they are not extensible. Singleton avoids this by keeping the instance of the class as a static member (and providing access to it with a static method), but the class itself is comprised of instance methods:



So the question is, what good is extensibility in this case? Even if you write a class Bar the extends Foo, Foo.getInstance() will always and forever return an object of class Foo (instance is a "new Foo()", not a "new Bar()").

In that case, you'd want to use the Abstract Factory pattern in combination with these two Singletons (Foo and Bar) to provide correct Singleton to other classes. In fact, usually when using Abstract Factory, you'd abstract the interface of the class returned by an AF so callers wouldn't know the class of the object they're using at all (they work with the interface, so they only know the type).

Check out this article over at Object Mentor for an interesting alternative to Singleton called the Monostate pattern.

sev
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between the singleton pattern class and static class
 
Similar Threads
Static methods
Singleton Object
SingleTonpattern and Static methods
Singleton Class static vs non-static methods
constructors marked private