24

I was just wondering...

why can i have only one instance of Calendar object. Is there a reason for it to be a singleton?

I have tried to read the documentation but they didn't mention why this is needed. And a quick google search didn't give me any answers.

2
  • 1
    Calendar is not a singleton and you can have multiple instances. Show us the code where you have a problem. Commented May 19, 2011 at 7:21
  • i don't have any problems with the code, i was just wondering ... Commented May 19, 2011 at 7:34

4 Answers 4

36

Calendar is not a singleton, it is an abstract class. The getInstance method is a Factory method that returns a concrete implementation of the Calendar class.

Search Google for java.util.Calendar source code, and you will see how it works.

Sign up to request clarification or add additional context in comments.

Comments

10

It is not singleton.

This:

public static void main(String args[]) {
        Calendar c1, c2;
        c1 = Calendar.getInstance();
        c2 = Calendar.getInstance();
        c1.add(Calendar.MONTH, 1);
        System.out.println(c1);
        System.out.println(c2);
    }

Outputs:

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Jerusalem",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=26,startDayOfWeek=6,startTime=7200000,startTimeMode=0,endMode=1,endMonth=8,endDay=13,endDayOfWeek=0,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=5,WEEK_OF_YEAR=21,WEEK_OF_MONTH=3,DAY_OF_MONTH=19,DAY_OF_YEAR=139,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=21,SECOND=27,MILLISECOND=839,ZONE_OFFSET=7200000,DST_OFFSET=3600000]
java.util.GregorianCalendar[time=1305789687839,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Jerusalem",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=26,startDayOfWeek=6,startTime=7200000,startTimeMode=0,endMode=1,endMonth=8,endDay=13,endDayOfWeek=0,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=3,DAY_OF_MONTH=19,DAY_OF_YEAR=139,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=21,SECOND=27,MILLISECOND=839,ZONE_OFFSET=7200000,DST_OFFSET=3600000]

(Which is different as you can see)

BTW, quick search for source code returns:

public static synchronized Calendar getInstance() {
       return new GregorianCalendar();
}

Comments

9

Did you think that it is a singleton because it has a getInstance() method? That's not the case!

getInstance() returns a new instance each time.

6 Comments

yes, exactly because of that. and now i feel dumb because of it :/
@Gabriel no, the API designers should feel dumb. It's a miserably designed API, which is why anybody with common sense uses Jodatime instead
"anybody with common sense and not bound by some stupid restrictions that require him to use the native classes" ... sigh.
@Joachim I know, I've been there too :-)
It's the normal case that get methods returning a mutable object return a new instance. static methods returning a shared instance of a mutable object should be burnt.
|
4

You can have as many instances of Calendar as you want ... modulo that it is an abstract class, so you are talking about of instances of child classes of Calendar.

Perhaps you think that the getInstance() method returns a singleton object? It doesn't. It creates and returns a new object each time you call it.

(The javadoc doesn't explicitly state that the calendar is not a singleton, but it says "The Calendar returned is based on the current time ...". That implies that it is returning a new object each time ... because the current time keeps changing. And anyway, that's what the method does if you'd care to look at the source code.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.