• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

copies of objects

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, i'm stumped and hoping that someone can help me out...
i've created a class called IntState, which stores the state of a system
a second class Optimization performs an optimization algorithm on an original system state to find the state of optimum reliabilty... however, i'm having a major problem...

when executing the following (psuedo)code, the method makes changes to the currentState object as well as the state object, even though currentState is never modified, and is used only to supply a copy.

public IntState getNextState( IntState currentState )
IntState state = currentState.copy();
modify state
return state


anyone have any ideas??? i'm getting really desperate and would really appreciate it...
thanks in advance.
mike
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your copy() have the same functionality of a clone() method? If so why not use a clone() method instead? Are you using deep or shallow copying? I think you have to be cafeful if you're deep-copying.
[ April 07, 2005: Message edited by: Yevgeniy Treyvus ]
 
Michael Lipton
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i've made a little progress, i think the problem is that i'm making a shallow copy, but i need to be making a deep copy... do you know of any simple ways to do this??
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sun has a tech tip article on making 'deep copies' that might interest you:
http://java.sun.com/developer/JDCTechTips/2001/tt0410.html#making
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,

Here is a method that I wrote in one of my framework utilities. This is a simple way of making a deep copy. If performance is critical, there are faster ways of doing this.........but this should get you past your problem.



Hope this helps.

-Ed
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can use it, Jakarta Commons Lang has a SerializationUtils.clone() method that would be of use to you.
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic