简体   繁体   中英

calendar.getInstance() - how it return object

The doc here at Oracle says

Calendar class is an abstract class.

The same doc also says

Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time: Calendar

rightNow = Calendar.getInstance();

After going through the discussions at this post of stackoverflow I understand we can not create object of abstract class.To create an object of abstract class we must implement its concrete subclass.

Can somebody please help me understand ( or point me to some references) why we can create an object of Calendar class even the class itself is an abstract class?
OR
I am missing some very important points about abstract class

getInstance() internally creates an object of some subclass that's hidden somewhere. (If you search down in its implementation, it ends up creating instances of subclasses like GregorianCalendar and returning that as a Calendar .)

Static factory methods like this allow APIs like Calendar to internally choose how to create the instance -- whether to create a subclass instance, or to cache one instance for the whole class, or whatever. This is a great design practice.

getInstance() returns some object of type Calendar. It means, that this object is instance of Calendar or its child. As you can see here:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/Calendar.java/

Calendar.getInstance() returns an instance of GregorianCalendar

What the method signature public static Calendar getInstance() declares, is that by calling it, the implementation code returns an object which you can safely assign to a variable of type Calendar. You don't need to know which actual subclass of Calendar you get and in fact later versions of Calendar could return a different subclass (than the GregorianCalendar the current returns) if needed, without breaking client code.
Think of the abstract type Calendar as an interface (with some implementation) for this case: A method can also declare to return an interface type although you cannot create explicitly an object of the interface class.
As of the fact that we call a method of an abstract class, since the method is static, it belongs to the class, not to any object of the class. There is no need to create an object to call it.

getInstance() is a static method. You do not need an instance of Calendar to call the method. Static methods do not need instances of the class to call them. The static method creates an instance of a child class and returns it that is why it is possible.

Example:

//Class has static method print()

Class object = new Class();  // I don't need to do this to call print
object.print();  //Not needed but acceptable

Class.print();  // I can directly call print() from the class without an instance because it is static

I hope I understand your question correctly, your question is about the object returned by Calendar.getInstance()?, why don't you write a small test case to find out?, do:

@Test
public void testCalendar(){
   Calendar calendar = Calendar.getInstance();
   final String name = calendar.getClass().getName();
   Assert.assertThat("Calendar is a  GregorianCalendar",name, Matchers.is("java.util.GregorianCalendar"));
}

The link is clear about why we refer to the base class or the interface and never to the concrete classes.

Please take a look also in: http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html

Calendar.getInstance() is a native method, and note down native methods don't have body/implementation. They have implemented already from the system.

It writes here: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html .

And, as explained in above answers already, the calendar instance is created by calling Calendar class. Static Factory design pattern to create calendar instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM