Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a Java method modifies an object passed as parameter

I come from a C++ background and I am currently learning Java. One question arose when I have tried using some third party libraries. How do I determine if the call to a method taking an object reference as parameter modifies the object? In C++ this is clear thanks to the use of the const keyword. If the method signature is:

void foo(Boo& boo); 

I know that the referenced object might be modified, while if the method signature is:

void foo(const Boo& boo); 

The compiler guarantees that the referenced object is not modified.

I haven't seen something analogous in Java, as only the reference itself can be declared final, not the referenced object, and a final argument doesn't make much sense in the first place since it is passed by value anyway. Therefore, when I see a method such as:

void foo(Boo boo) {...} 

How do I determine if the object referenced by boo is modified inside the body of the function (maybe using annotations)? If there is no way to know, is there some widely used convention or some best practices to avoid confusion and bugs?

like image 542
ItalianMonkey Avatar asked Aug 23 '15 07:08

ItalianMonkey


People also ask

When an object is passed as a parameter into a method?

To allow a method to modify a argument, you must pass in an object. Objects in Java are also passed by value, however, the value of an object is a reference. So, the effect is that the object is passed in by reference. When passing an argument by reference, the method gets a reference to the object.

How objects are passed as function parameters in Java?

Objects are implicitly passed by use of call-by-reference. This means when we pass primitive data types to method it will pass only values to function parameters so any change made in parameter will not affect the value of actual parameters.

Can a method take an object as a parameter?

You can use any data type for a parameter of a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as objects and arrays.

What happens when you pass an object to a method in Java?

Java always passes parameter variables by value. Object variables in Java always point to the real object in the memory heap. A mutable object's value can be changed when it is passed to a method. An immutable object's value cannot be changed, even if it is passed a new value.


2 Answers

how do I determine if the object referenced by boo is modified inside the body of the function (maybe using annotations)?

The only way is to read the code unfortunately.

If there is no way to know, is there some widely used convention or some best practices to avoid confusion and bugs?

The common convention is to pass an object which cannot be modified, using a wrapper if needed. This ensure the class cannot modify the object.

List<String> readOnly = Collections.unmodifiableList(list); 

If the object is Cloneable, you can also use clone() but another common approach is to use a copy.

List<String> readOnly = new ArrayList<>(list); 

If you care about such behaviour, unit tests can show whether a method modifies an object or not. If you have unit tests already, it is usually one or two lines extra to check for this.

like image 159
Peter Lawrey Avatar answered Oct 12 '22 04:10

Peter Lawrey


There's no such facility built in to the language, unfortunately. A good defensive practice is to define the data objects you pass around as immutable (i.e., without any public method that allows modifying their state). If you are really concerned about this, you could copy/clone an object before passing it to a method you don't trust, but this is usually a redundant precaution.

like image 37
Mureinik Avatar answered Oct 12 '22 05:10

Mureinik