• Post Reply Bookmark Topic Watch Topic
  • New Topic

[SOLVED]Property gets altered by altering it's copy  RSS feed

 
J. Frederik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just don't understand why the property orderValueAndDeliveryFees gets altered by altering it's copy...

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Frederik wrote:I just don't understand why the property orderValueAndDeliveryFees gets altered by altering it's copy...

Well, from what you've supplied, neither do I; but I'll bet dollars to doughnuts that the reason is that what you think is a "copy" is actually just another reference to the same List.

Winston
 
J. Frederik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer.
Ok, so I am not totally stupid thinking alike setups were working before.
Actually I am still quite clueless where to look... What possibilities are there for this not being a copy?

How can I find out if this is just a reference and *cough* were can I read more about Java references (it's a term hard to search for)?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the method looks anything like this:

then you are not returning a copy. Returning a copy would require something like this:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Frederik wrote:
How can I find out if this is just a reference and *cough* were can I read more about Java references (it's a term hard to search for)?


Apart from primitives, most everything else you deal with in Java is a reference to an object. The terms "object" and "object reference" are synonymous most of the time. It's only when you are dealing with issues like this where you have to make a distinction between the actual area of memory occupied (the object) vs the "pointer" (the reference) used to access that memory.

In other words, wherever you have code that, for example, returns an object, it's actually returning a reference to that object. A new object is never created by simply creating a new reference to an existing object. The only way to create a new object or a copy of an object is to invoke a constructor, a factory method (which must eventually invoke a constructor), or the clone() method, if it's implemented by the class, to allocate more space in memory.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I recall correctly, Bruce Eckel's book "Thinking in Java" has a good discussion about objects and object references. The book might even be available for download as a PDF.
 
J. Frederik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer. It seems the problem is solved due to it.
I changed the line to

and now it looks fine.
I never realised this makes such a difference. I really have to look through my other code for alike mistakes.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on the requirements, that fix may or may not be good enough. If you find yourself duplicating that fix in more than one place, that would be an indication that the design of the getter method needs to be changed from returning a reference to returning a copy. Read Joshua Bloch's book "Effective Java" for more on this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!