Pass Parameter In Array

JavaPassesByValue, but sometimes you want PassByReference.

Therefore: Pass the parameter wrapped in an array:

  void addOne (int[] arg)
    {
    arg [0] = arg [0] + 1;
    }

...

int a[] = new int [1] {2}; addOne (a); System.out.println (a [0] );

3

However: This smells funny, so see AlternativesToPassByReference

See also: PassParameterInWrapperObject


CategoryJava
EditText of this page (last edited July 6, 2004)
FindPage by browsing or searching

This page mirrored in JavaIdioms as of April 29, 2006