Alex Rivera | Logout

Is it a good practice to change arguments in Java

Asked 2012-08-18T15:54:24.330
20

Suppose I am writing a method foo(int i) in Java.
Since i is passed by value it is safe to change it in foo. For example

void foo(int i) {
   i = i + 1; // change i
   ...
}

Is it considered good or bad practice to change arguments of methods in Java?

Edit
Report

1 Answer

3

Since i is passed by value it is safe to change it foo()

It is absolutely safe even when you pass an object reference because they are local: i.e., assigning a new reference to the local reference will not have any impact on the original reference in the calling code

It's your personal choice. However i would not change the argument values as one might loose the track of the actual value passed in to this method.

answered 2012-08-18T16:02:37.260

Your Answer