Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variable Question

 
Will Blanc
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
calendarOne.get(Calendar.YEAR);
calendarTwo.get(Calendar.YEAR);

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,
Will
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you paste the complete code block please?
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Will Blanc
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh! Thank you everyone!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic