With the question of passing an object as a parameter by value, everything seems to be clear - in the example below, the code in the actionTimer method actionTimer not affect the timer object [0].

But the question arises, how can you solve such a problem - work with the same type of objects in the same way, using the same method? Those. What working alternatives can be used to implement the problem solved in the code below?

 public class MainActivity extends Activity { private Timer []timer = new Timer[10]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mainactivity); actionTimer (timer[0]); } private void actionTimer (Timer localTimer) { localTimer = null; localTimer = new Timer(); } } 

Or is there only one solution - for each object to use its own method, for example, as in the code below?

 actionTimer1 (); } private void actionTimer1 () { timer[0] = null; timer[0] = new Timer(); } 
  • one
    It is not clear what you are trying to achieve. If you want the actionTimer method actionTimer change timer[0] then pass 2 incoming parameters - the timer array and int index 0. - anber
  • @anber Thanks, your way worked! - Doraemon

1 answer 1

function call

 actionTimer (0); 

function

 private void actionTimer (int index) { if (index < 10 && index > 0) timer[index] = new Timer(); else // опционально, но полезно throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); }