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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)