#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    29
    Rep Power
    0

    Java Declaration


    I m hoping someone can pseudo code the following declaration, to clear things in my head:

    ========================

    Calendar startTime = new GregorianCalendar();

    ========================

    Am I right in saying the above snippet means the following:

    make 'startTime' an Object of the method 'GregorianCalendar' which is in the ' Calendar ' api?

    Thanks for all help,

    Santos
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    5
    Rep Power
    0
    err, that seems very wrong to me.

    Calendar startTime = new GregorianCalendar();

    should be
    Calander startTime = new startTime()

    you cannot replace an object with a method!
  4. #3
  5. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Eh.... Yes, well, here's the answer:

    There is a class called Calendar. It's an abstract class, which means you can't actually *make* a Calendar object. It has a subclass, though, called GregorianCalender. Now, GregorianCalendar is still a Calendar, ie: anything a Calendar can do a GregorianCalendar can do too. A GregorianCalendar has been extended, though, to include extra functionality.

    So here's what your code is saying:

    Make a new Calendar object, called startTime, using the GregorianCalendar class.

    Creating an object this way means that the Java compiler will recognize startTime as a Calendar, so if you try to call methods from the GregorianCalendar class you'll get a compiler error. You can cast startTime to a GregorianCalendar object, though, if you need those methods.

    A note about default constructors:

    Calendar startTime = new GregorianCalendar();

    GregorianCalendar() is a function of the GregorianCalendar class that basically says "Make a new object". It has no arguments and has the same name as the class. It's called a default constructor, and every class has one.

IMN logo majestic logo threadwatch logo seochat tools logo