• 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 ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why is calendar object created using getInstance()

Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting the reason behind why calendar class is created using getInstance() ? Is it something to do with the design ?
Are there any specific reason behind it??

Why did they not have the constructor similar to Date class?

Please spare me if this is really silly question
Posts: 4568
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a silly question at all, but there is a very good reason.

It uses what's called the Factory Pattern, which allows you to create an object without knowing what specific type it is. Calendar is an abstract class, and you're actually getting an instance of a sub-class. Which subclass can vary, but the person using the Calendar doesn't need to worry about the details. Whereas to use a constructor you need to know the actual class.

If you're interested in more detail, it can be useful to look at the source code. It turns out the type returned by getInstance() depends on what Locale you're running in. Under normal circumstances you get an instance of GregorianCalendar. But under the right locale, it can return a BuddhistCalendar or JapaneseImperialCalendar! All these are subclasses of Calendar.
    Bookmark Topic Watch Topic
  • New Topic