| Author |
Why is calendar object created using getInstance()
|
Kowshik Nandagudi
Ranch Hand
Joined: Dec 09, 2010
Posts: 57
|
|
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
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3791
|
|
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.
|
 |
 |
|
|
subject: Why is calendar object created using getInstance()
|
|
|