2

Is it possible to set the default behavior of my Java project (or some subset of it) to make copies of my objects when I pass them into methods or functions?

The reason for wanting this behavior is that I am writing a number of tests that check that two functions produce the same return. If I pass in ObjectA (where a property count is initialized to 3) to function decrementCount() than when it returns the object I will see (ObjectA.count -> 2), while after calling the second function the object is now (ObjectA.count -> 1).

I would like to achieve pass-by-value rather than pass-by-reference.

Alex Bollbach
  • 4,370
  • 9
  • 32
  • 80

1 Answers1

3

Is it possible to set the default behavior of my Java project (or some subset of it) to make copies of my objects when I pass them into methods or functions?

No, Java is only pass by value. Java's variables can only be primitives and references. They cannot be Objects, and you can't force these to be copied (without copying them explicitly).

If I pass in ObjectA (where a property count is initialized to 3) to function decrementCount() than when it returns the object I will see (ObjectA.count -> 2), while after calling the second function the object is now (ObjectA.count -> 1).

I suggest you recreating the object for each test. And if you want to prevent modification, you can test it wasn't modified.

I would like to achieve pass-by-value rather than pass-by-reference.

This is in fact your only option. However the copy of the value in the reference is not a deep copy.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • If I understand correctly, you are saying java is pass-by-value. However, when I think of pass-by-value, I think of copying. In this case it is the reference itself that is copied? So this isn't the kind of copying I was implying in my initial question? – Alex Bollbach Apr 25 '16 at 14:05
  • 1
    Java is passing *references* by value, not the objects they reference. It's a subtle distinction, I know. – Hank D Apr 25 '16 at 14:07
  • @user5797668 yes, it a common misconception that if you write `String s` that `s` is a String object, it is not. `s` is a reference to a String and if you assign it or copy it you only copy that reference. If you do `s == t` you compare references, not their contents. if you do `s = null;` you don't destroy the String, you are setting a reference to `null`. There is no special symbol in Java to say it is a reference because there is no other option. – Peter Lawrey Apr 25 '16 at 14:09
  • I was aware that references simply point to existing data structure elsewhere called objects. However, I was unsure as to whether this `@504` reference type was copied or simply passed. Therefore, to say it precisely, it is stated as a "reference, which is passed by value"? as opposed to a "reference which is passed by reference". Either way, a deep copy of the object is my ultimate goal. – Alex Bollbach Apr 25 '16 at 15:10
  • @user5797668 references are passed by value, I think your ultimate goal should be to write tests which don't need the objects to be copied. You can just create new objects as needed. – Peter Lawrey Apr 25 '16 at 16:10
  • One reason I feel i need to copy the object is because often the first function will modify the structure of the input object such that inputting the same object into the second function will return a different object even if both functions are identical. Now, I'm just recreating the object in code, but a clone() function that worked as a deep copy would save me those lines of code is all. – Alex Bollbach Apr 25 '16 at 19:03
  • 1
    @user5797668 I suggest you create a method to generate your test data and you won't have repeated code. – Peter Lawrey Apr 25 '16 at 19:06
  • @user5797668: There is actually no concept of "copying an object" at the language level. Some objects may choose to provide a public method to return a reference to an object that is a "copy" of itself; but there is nothing special about such a method vs any other method, and no way to tell whether an object has such a method or what it may be called, other than to read its documentation. There is no general way to "copy" objects in Java. – newacct Apr 25 '16 at 22:05