This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.Calender

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • 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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks rob
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys

i think these seneario applies to

java.security.MessageDigest
 
Raghu ram
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes ragu thanks for the reply

regards
amir
 
hem kumar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • 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.....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic