• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
Rommel Sharma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 61696
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!