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 Why is calendar object created using getInstance() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why is calendar object created using getInstance()" Watch "Why is calendar object created using getInstance()" New topic
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
    
    1

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is calendar object created using getInstance()
 
Similar Threads
help understanding Calendar constructor vs method
new MyClass() or MyClass.getInstance()
Why is Calendar an abstract class ?
Abstract Classes
abstract class instance