This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static variable Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static variable Question" Watch "Static variable Question" New topic

Static variable Question

Will Blanc

Joined: Jul 11, 2008
Posts: 8

Assuming calendarOne and calendarTwo have different years, why does each line return a different number?--I thought all objects of a class share the static variables of a class, so doesn't that mean CalendarOne and CalendarTwo would share the same Calendar.YEAR, since YEAR is static? But it seems like they don't b/c each Calendar.YEAR is returning a different year. How it that possible?

Thank you,
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
Can you paste the complete code block please?

My Techie Blog - Java Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41084
Because Calendar.YEAR is taken to mean, "I want you to work out which year is in the data." It does not mean the value of the year.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14599

I'm not sure I understand exactly what you mean.

It works like this: Class Calendar has a get() method. You can use this to get different values that are set in the calendar object - for example the year, month or day that the calendar is set to. You pass a constant such as Calendar.YEAR to the get() method to tell it which value to return.

So if you call calendarOne.get(Calendar.YEAR), then the get() method sees "aha, he wants me to return the year that this calendar is set to".

Calendar.YEAR is a constant, but each calendar object can ofcourse be set to a different date. You're confusing the meaning of Calendar.YEAR and the current date setting of the calendar object.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3870

Calender.YEAR,Calender.MONTH etc.. can be considered as constants which identifies the property you are trying to get. So to identify each properties we can use these constants as provided. But each instance has it's own values as you set them and Calender.YEAR means same property in each instance (say year etc).

[How to ask questions] [Twitter]
Will Blanc

Joined: Jul 11, 2008
Posts: 8
Ahh! Thank you everyone!
I agree. Here's the link:
subject: Static variable Question