File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Singleton class having a Static declaration in  its method which returns its instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton class having a Static declaration in  its method which returns its instance" Watch "Singleton class having a Static declaration in  its method which returns its instance" New topic
Author

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

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .






Save India From Corruption - Anna Hazare.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
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?


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Sep 16, 2008
Posts: 241
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 ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
You could also use a one-member enum.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Sep 16, 2008
Posts: 241
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

Joined: Apr 18, 2009
Posts: 2234

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 .



Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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.. )


OCPJP 6
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18132
    
    8

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

If you want singletons, check out java.lang.Runtime, java.awt.SystemTray or java.awt.GraphicsEnvironment.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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
Bartender

Joined: Oct 14, 2005
Posts: 18132
    
    8

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

Joined: Apr 18, 2009
Posts: 2234

Thank you i finally got the answer which has been a query for me since long time .

Thank you each one for your time .
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

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.
 
 
subject: Singleton class having a Static declaration in its method which returns its instance
 
Similar Threads
Questions About Being Static
Singleton Question
Java Singleton Pattern Tutorial
Understanding singleton pattern
implementing singleton pattern in an interface