• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

java.util.Calender

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys
iam working in a maintenace project
this is the first time iam using Calender.when i saw the api
of java.util.Calender i found that it is an abstract class and i have seen
a statement in a java class

Calendar rightNow = Calendar.getInstance();

how can this getInstance() method can return a calender object since abstact class cannot be instantiated

however java.util.Calender is not extended by any java class in my project
they have imported this statement

import java.util.Calendar;

i need your valuable sugesstions
we are using jdk 1.5.0.6

regards
amir
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line does not mean that the getInstance method returns a Calendar object. It means that it return an object whose type extends the Calendar class.
More generally,means that classMethod returns an object that is a ReferenceType object (if ReferenceType is a concrete class), returns a object that is a subclass of ReferenceType (if ReferenceType is concrete or abstract), or returns an object that implements ReferenceType (if ReferenceType is an interface).

If you add the lineafter that line, it will tell you what the class of the returned object actually is.
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This happens a lot, for instance with java.awt.Toolkit and java.text.DateFormat.

Calendar itself is abstract, but the static method returns an instance of a non-abstract subclass of Calendar.

In the case of Calendar, the returned calendar is either a sun.util.BuddhistCalendar (this is a Sun internal class), a java.util.GregorianCalendar or (in Java 6 and up) a java.util.JapaneseImperialCalendar (which is package private). In the case of Toolkit, this is some internal class.

What you need to remember from all of this is: the method returns some unknown subclass, and the only thing you are sure of is that it is a Calendar / Toolkit / DateFormat / etc. You should never count on the specific subclass (like GregorianCalendar), since it may change in the future.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks rob
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys

i think these seneario applies to

java.security.MessageDigest
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello amrithraj,

getInstance is a static method. so you are not instantiating (not creating object) juz using reference you are returning using getInstance method.

I think you are confused with instantiation and refference.

suppose you have an abstract class called Mycalender and a static method called getInstance(), So

If you are instiating Mycalender m = new Mycalender(); /* error */

Mycalender m = Mycalender.getInstance(); /* no error */

becasue m is an reffernce variable not an object

getInstance is a static method so static methods can also invoked using class name if you give like this Mycalender.getInstance() is correct.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes ragu thanks for the reply

regards
amir
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its kindly requested to both raghu ram and amir please make sure your spelling is correct. First for both raghu ram and amir - its not Calender class, its Calendar class. And secondly, for raghu ram - its not refference its simply reference. YOUR correct spelling will make the job of others way easier. Thank you.....
 
reply
    Bookmark Topic Watch Topic
  • New Topic