• 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

Singleton class having a Static declaration in its method which returns its instance

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are having some Singleton classes in our Application which will get all the static maintaince stored in database.

Any class which require this data , just calls this calls by

MaintainceCacheData filewrapper = MaintainceCacheData.getInstance();

And inside the MaintainceCacheData class which is a singleton class has a static method in it as shown :



My question is why is it necessary to have a Method as static declaration for a singleton class whcih returns its instance .

Please share your knowledge .

Thanks for reading .





 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing, given the factory like implementation, that this singleton class has a private constructor. If this method wasn't static there would be no way of instatiating this class (outside of it). That would make virtualy it unusabe, wouldn't it?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya Martin , this has a private constructor .

I got my answer on to the above question .

If you dont mind i got an another question , why is it necessary to have a private constructor inside a Singleton class ??

 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the name implies, you wish to have only one instance of the object at the one time when you implement a class as a Singleton. You want to have control over how many instances there are (just one ). You achieve that by making instantiation controlled by the class itself, thus making all constructors private and a static method that instantiates a new object only of there are none already present. Probably overriding clone() will help too. Some consider Singleton an anti-pattern ...
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also use a one-member enum.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As the name implies, you wish to have only one instance of the object at the one time when you implement a class as a Singleton.



Martin , Thanks very much for the explanation . Actually My question is "Why we need to have only one instance for a Singleton class"??
Why cant we leave this with a public constructor , so that if whenever any class requires this Object's data just instainate with new keyword and call a method on it

For example

In another class :



what is the problem with this ??


 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really can't answer that. Someone once decided that your MaintainceCacheData class should be a singleton and implemented it that way. You will have to got through code, to determine what will be the results of making that class instantiable. Judging by the name it's some sort of chache. If that is so it probably makes sense that there is only one.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Martin for the response .

Can anybody please answer this question in general terms of a Singleton class (not with respect to my class) i mean any Singleton classs .

Thanks .



 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure I understand your question. The reason you would use a singleton is because you've made a design decision that there must be only one instance of this class in your application. Why that design decision was made is not something I can say. It presumably has some feature (probably some data) where more than one version would be invalid? But I'd ask the person who wrote the application. They are in a far better possition than us to answer why they did what they did.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul thanks for your time .

This is my question exactly .

In what cases do we need to have only one instance of aclass in a application.

Please let me know .

Thanks in advance .
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Kiran V wrote:Actually My question is "Why we need to have only one instance for a Singleton class"??


Because if you had more than one it wouldn't be a singleton.


Why cant we leave this with a public constructor , so that if whenever any class requires this Object's data just instainate with new keyword and call a method on it

For example

In another class :



what is the problem with this ??


Potentially nothing--depends on how it's implemented.

Just because singletons are used doesn't mean they're a good idea (and I'd argue that in most cases, they're not, at least as typically done in Java). They making testing more irritating than necessary, but that has more to do with the usual way of using them.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravi, see if this helps....how would you instantiate the java.util.Calendar class - which we all regularly use?!

Like this,right?

The Java designers decided to make Calendar class a singleton class, because all you need is some info/manipulation on date/time, for that why do you need more than 1 object?
You get me?



Do you really need 2 objects to manipulate on date/time??You can ofcourse have 2, but it is just a overhead!

Why have many when one would do?(I'm talking about Objects not Wives.. )
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:Ravi, see if this helps....how would you instantiate the java.util.Calendar class - which we all regularly use?!

Like this,right?

The Java designers decided to make Calendar class a singleton class...



No, they didn't. That's an example of the Factory pattern, where Calendar.getInstance() returns a new instance of some subclass of Calendar which is the right one for your environment. Typically it's GregorianCalendar but it could be something else.

And it's perfectly reasonable to have two Calendar objects. They could contain different information, for example.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want singletons, check out java.lang.Runtime, java.awt.SystemTray or java.awt.GraphicsEnvironment.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:
And it's perfectly reasonable to have two Calendar objects. They could contain different information, for example.



Is it possible to create 2 instances of a Calendar?? I've never seen such code! How to create 2 of them? Enlighten me please.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:

Paul Clapham wrote:
And it's perfectly reasonable to have two Calendar objects. They could contain different information, for example.



Is it possible to create 2 instances of a Calendar?? I've never seen such code! How to create 2 of them? Enlighten me please.


It's not terribly difficult...

And there you have it. Two Calendar instances.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you i finally got the answer which has been a query for me since long time .

Thank you each one for your time .
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of subtle technical problems related to singletons and multi-threading in the very early initialization of the class. For a detailed explaination, and coding styles to fix it, see
http://www.ibm.com/developerworks/java/library/j-dcl.html

I believe that the Singleton pattern is the most missused design pattern in all of Object Oriented Programming. It is widely popular, but should not be.

"Singletons considered harmful" should be the title of any reference to them.

What a singleton really is to the world is a big, glob of public data that greatly increases coupling between modules. It make proper unit tests impossible.

There are much better design patterns that are easy to implement and avoid the problems of the evil singleton pattern.

The Gang of Four got this one wrong.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic