• Post Reply Bookmark Topic Watch Topic
  • New Topic

getInstance()  RSS feed

 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would I use MyClass.getInstance() instead of instanciating a class with New MyClass()?

Thanks, Lisa
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because sometimes the constructors are all private, and you cannot call them.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you would like to instantiate your MyClass in the following manner :

MyClass(int colourCode);
MyClass(int shade);

Though the example might not be good but the point is you can have only a single constructor with an int param in a class. But in case of getInstance you can have getMyClassColourInstance().

Secondly you may want to cache immutable objects incase they already exist. So in case an Object is already present you may want to return the reference to the same Object and not create a new one.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to follow up on what Anupam said:
Secondly you may want to cache immutable objects incase they already exist. So in case an Object is already present you may want to return the reference to the same Object and not create a new one.

which is the case when you have a Singleton class.

/Svend Rost
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen a case made to make every class a factory for itself. If I write

all over my application, it will be hard to plug in a new implementation or extension of MyThing. If I write

I can easily change getInstance() to return SonOfMyThing or any new implementation that I might read from configuration.

Doing this on every class would seem to be overkill, but how confidently can we predict what classes we might like to make plug-ins later on? And what's the cost? One single-line method?
[ February 27, 2007: Message edited by: Stan James ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised that nobody mentioned the singleton pattern yet.

Sometimes, when you write a class, you need to have control over how many instances of the class are made. For example, you might want to have it so that there is only one instance of the class, which is shared by all the users of the class. That's called the singleton pattern (link to a longer explanation above).

If you have one or more non-private constructors in your class, then the class does not have control over how many instances of the class are created - other classes can use "new ClassName" to make as many instances as they like. To get control over the creation of instances, you need to make all the constructors of the class private - which means that only methods in the class itself can access those constructors.

One common way in Java to implement a singleton class is to make all the constructors of the class private, and then to provide a static factory method like the getInstance() method you mentioned to get the single instance of the class.
[ February 27, 2007: Message edited by: Jesper Young ]
 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help. I have a much better understanding of where I would and would not use getInstance.

Thanks, Lisa
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
I'm surprised that nobody mentioned the singleton pattern yet.
[ February 27, 2007: Message edited by: Jesper Young ]

Im surprised as well, as I though we already had mentioned it

/Svend Rost
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Svend, overlooked your post...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!