Is it problematic to assign a new value to a method parameter?

后端 未结 8 558
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 01:17

Eclipse has an option to warn on assignment to a method\'s parameter (inside the method), as in:

public void doFoo(int a){
   if (a<0){
      a=0; // this         


        
8条回答
  •  情深已故
    2020-12-10 02:00

    Different compiler warnings can be appropriate for different situations. Sure, some are applicable to most or all situations, but this does not seem to be one of them.

    I would think of this particular warning as the compiler giving you the option to be warned about a method parameter being reassigned when you need it, rather than a rule that method parameters should not be reassigned. Your example constitutes a perfectly valid case for it.

提交回复
热议问题