aspose file tools*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static variable Question" Watch "Static variable Question" New topic
Author

Static variable Question

Will Blanc
Greenhorn

Joined: Jul 11, 2008
Posts: 8
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

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


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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: 13884
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Will Blanc
Greenhorn

Joined: Jul 11, 2008
Posts: 8
Ahh! Thank you everyone!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static variable Question
 
Similar Threads
a problem with Date class
Need help writting an expiration date
getting the start date for a week number
add one day to current date
Calendar problems