Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static variable Question  RSS feed

 
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
Marshal
Posts: 54881
155
  • 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
Sheriff
Posts: 15985
86
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: 3976
27
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!
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!