wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Calendar's set method returns unexpected result on multiple invocations with different inputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar Watch "Calendar New topic
Author

Calendar's set method returns unexpected result on multiple invocations with different inputs

Rommel Sharma
Greenhorn

Joined: Oct 31, 2003
Posts: 17
Hi,

I am calling the set method of Calendar class
i.e. public final void set(int year, int month, int date)
twice from a class (say EmployeeManager) setting two different sets of values (say 5th March 2012 and 25th Feb 2010) and passing the values to the constructor of another class (say Employee) invoked for two different objects created of that (Employee) class. The set method is called the 2nd time only after it is passed the first time to the first constructor of the Employee class.

However when I retrieve the values, using a getter method of the Employee class I get the same date (I get back: 25 - Feb - 2010, that was passed the 2nd time).

Program and the output below:



This gives me:




The problem does gets fixed if we take take two different Calendar instances and use them e.g.




Why is the object of the Employee class not holding in its two different objects two sets of values specified using Calendar's set method when the Calendar instance is the same but values set are different each time?

Many thanks in advance for the response!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7699
    
  20

Rommel Sharma wrote:The problem does gets fixed if we take take two different Calendar instances and use them e.g.
...
Why is the object of the Employee class not holding in its two different objects two sets of values specified using Calendar's set method when the Calendar instance is the same but values set are different each time?

I would have thought that your first statement, combined with your test, would have pointed you to the answer:
Your Employees are not holding two different objects; they are holding two different variables, each of which is a reference to the same Calendar object. And since Calendars are mutable, the value you see will be the one produced by the last set() call that was run on it.

If you want to keep your Calendars separate, you have no choice but to create a new one for each Employee.

Winston

PS: Please DontWriteLongLines (←click). I've broken yours up this time (the comment line in your last code snippet).

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rommel Sharma
Greenhorn

Joined: Oct 31, 2003
Posts: 17
Thanks Winston, was back to the basics. Wrote a simple java program for discussing this in general, where custom classes involved are all mutable.
(so this question is not attributed to any particular class or method i.e. Calendar, set(...) ).




Shows:



thanks a lot!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
You have fallen into a standard pitfall. The second reference to a single object is called an alias. You should have seen that you can get round the problem by using defensive copies whevern you let that mutable object into or out of your object.That example depends on the Bar class having a copy constructor. There are other ways to copy objects. There is no need to mess around with copies for classes like String, because String is immutable.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calendar's set method returns unexpected result on multiple invocations with different inputs