wood burning stoves 2.0*
The moose likes Java in General and the fly likes copies of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "copies of objects" Watch "copies of objects" New topic
Author

copies of objects

Michael Lipton
Greenhorn

Joined: Jul 22, 2004
Posts: 5
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
Yevgeniy Treyvus
Ranch Hand

Joined: Mar 09, 2005
Posts: 48
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 ]

SCJP, SCJD
Michael Lipton
Greenhorn

Joined: Jul 22, 2004
Posts: 5
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??
Hung Yee
Greenhorn

Joined: Dec 22, 2004
Posts: 18
Sun has a tech tip article on making 'deep copies' that might interest you:
http://java.sun.com/developer/JDCTechTips/2001/tt0410.html#making
Ed Wallen
Ranch Hand

Joined: Feb 11, 2002
Posts: 34
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
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
If you can use it, Jakarta Commons Lang has a SerializationUtils.clone() method that would be of use to you.


James Carman, President<br />Carman Consulting, Inc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copies of objects