• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

getInstance()

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can u exaplain bit about getInstance,
when do we need to use???

Calendar _calendar = Calendar.getInstance();// y do v use here

explain with more information.

Thz,
Chiyan
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getInstance is usually a method that you have in a "Factory" class. There is a design pattern that is called "Factory Method" pattern, which has a factory class that returns instances of a class. Instead of creating the class through "new", you go through the Factory and call its getInstance() method.

There are many reasons to use this design pattern and a discussion like that, I am sure you can find in our UML/Design Patterns forum. Just do a search in that forum for Factory.

Mark
 
Mark Spritzler
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also used in the Singleton pattern as well.

Mark
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to what's already been said, here's why you need to use getInstance with a Calendar object.

If you look up the documentation for the Calendar class, you'll notice that its constructors have the "protected" access modifier. In other words, no code that lies outside of its package (the "java.*" package) can call the constructor. And if you can't call the constructor, you can't use "new" to create a new instance of the object.

So how do you get a new instance if you can't call the constructor? You call a static, class method--in this case, that's getInstance(). That method is allowed to call the constructor, since it's part of the same class. It then passes a reference to the new object back to you.

A class is typically set up this way when a central point of control over the creation of the object is needed for some reason. For example, the class might look around to see if the object has already been created. If it has, it will just pass you a reference to that already-existing instance. If not, it will create one for you. (That's example of the "Singleton" pattern Mark mentioned, by the way.)

- Jeff
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jeff
is this object eligible for GC
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please anybody explain singleton pattern where there is a private constructor and we need get the instance through the getInstance() method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!