0

I thought to clarify the following issue:

If I have a mutable reference in an immutable object and I am cloning the mutable reference before passing it to any caller object, will this not create many copies of the mutable referenced object in the JVM?

Is this suggested, or do we have any other practice to get away with this?

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
  • 2
    If you copy a *reference*, you have multiple copies of that reference, all referring to the same object. This is not the same thing as copying the object to which those references refer. If that doesn't answer the question, then you would be wise to clarify what you mean by presenting example code. – John Bollinger May 16 '18 at 03:33
  • I think this will help out : https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy – Rann Lifshitz May 16 '18 at 03:38

1 Answers1

0

Indeed this will create multiple copies/clones of the original mutable object, but all of those copies will be useful if other threads are using those. Otherwise, you can simply use the same copy within the same thread if not modifying of keep discarding the old ones if possible.