• 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

Is there any difference between singleton and static class?

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assume that we have a class with full of static methods. To access this methods we don't need to create a object.

Is there any difference between singleton object and this class?
which is better and how?
 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singleton isn't about not creating an instance, Singleton makes sure that there is only one instance of a class (like a FileSystemManager object for example).
A class with only static methods is usually called a "Utility class".
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Todd wrote:Singleton isn't about not creating an instance, Singleton makes sure that there is only one instance of a class (like a FileSystemManager object for example).
A class with only static methods is usually called a "Utility class".



John,

Nice explanation. Concise & to the point.

Thennam,

Singleton is one of simplest OO pattern to explain but one of the rather difficult one to implement (depending on your system, class loaders etc).

As far as comparison goes as John said, it is apples vs oranges, they have completely different tasks in a system.

 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While there are usually many more efficient ways to design OO applications without using the Simpleton design pattern, the pattern does have considerable benefits when compared to class operations, e.g. more flexibility.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply because if you use an interface for your class you need an instance which implements that interface. This is for me the main reason for using the singleton pattern. I have some utility classes but I don't want the rest of my code to know the implementation details but I don't want the create lot of objects. So I use the singleton pattern for generating 1 object which implements the interface.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic