Reputation:
I am a beginner Java programmer, and I've been reading about the Calendar class. It's an abstract class and all of its methods are static, but it is used as follows:
Calendar cal = Calendar.getInstance();
Where you use cal
to call methods like:
cal.setTimeinMillis(day1);
I'm confused about this. When calling methods against a class, why do we need a reference variable and how is this legal for static methods?
Upvotes: 4
Views: 10454
Reputation: 31699
setTimeInMillis() is not static. The reason you need to call a getInstance() method to return an instance is that there are several getInstance() methods, and you can call them for different time zones and/or different locales than the default. Which time zone or locale you choose has an impact on how the other methods work.
Upvotes: 1
Reputation: 14413
1) Is Calendar not Calandar
2) Calendar.getInstance()
will return some concrete implementation of calendar, probably GregorianCalendar
.
3) cal.setTimeinMillis(day1); is object method, not static.
Upvotes: 2
Reputation: 51030
Calendar cal = Calendar.getInstance();
Is not calling a constructor, it's just calling a (static) method that returns an instance of some subclass (of Calendar
).
Upvotes: 8
Reputation: 51062
Take another look at the documentation; most of the methods of Calendar that do anything interesting (other than the getInstance() methods) are not static and require an instance (which is indeed the container of the moment in time that the calendar represents).
Upvotes: 2