wood burning stoves*
The moose likes Beginning Java and the fly likes java.util.Calender Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.util.Calender" Watch "java.util.Calender" New topic
Author

java.util.Calender

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
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

Joined: Aug 05, 2005
Posts: 3446
    
  12
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.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
thanks rob
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
hey guys

i think these seneario applies to

java.security.MessageDigest
Raghu ram
Ranch Hand

Joined: Oct 19, 2007
Posts: 34
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

Joined: Sep 28, 2006
Posts: 236
yes ragu thanks for the reply

regards
amir
hem kumar
Ranch Hand

Joined: Sep 05, 2005
Posts: 97
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.....


.....For Every Problem There Is a Solution.....
Regards, Hem
 
Don't get me started about those stupid light bulbs.
 
subject: java.util.Calender