• 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

when we should go for a singleton class in java?

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I sometimes get confused between two below options i.e

1)declaring class as singleton
or
2)Declaring the variable as static(mainly those variable for which we want to share them across the class i, ndependent of object )

Here is my understading

The pupose of singleton class is to keep only one instance of singleton class across jvm. Agreed. But i am trying to think of reasons why we want to keep only one instance. There can be two reasons:-

1)Object might be expensive to create. So we just want to keep only one instance. Agreed in this scenario declaring instance variables as static does not solve any purpose.

2)We want to share the same state of object across application. I was thinking this is the main purpose of declaring the class as singleton. But it can be achieved simply by declaring the instance variables as static.

But looks like 1 is the main reason of delaring any class as static not reason 2 because it can be achieved with static variable also.


Is above understanding correct?

if yes why logger class is made singleton?

what i think logger was cretaed as singleton so that single log file is created not multiple log files. But again this could have been achieved with declaring the file variable as static in logger file.
?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes you need to make sure that there will exist only one instance of a class. In that case, you might consider making it a singleton. Just making a static variable somewhere and not making the class a singleton is not sufficient in that case - in principle, you could then still create multiple instances of the class, which will lead to trouble. (Note that a singleton usually uses a private static variable to store a reference to the single instance).

You might want to make a class a singleton if it represents some resource of which there exists only one. An example is the logger you are talking about, where there indeed one log file, so you'd want to have a single object that handles all the logging to that file.

Note that there is also some criticism on singletons - some people even call it an "anti-pattern". Singletons can make code difficult to test.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic