1 2 Previous Next 16 Replies Latest reply: Oct 3, 2008 8:03 AM by 807589 RSS

    Passing Integer as argument to modify the value

      I want to modify an int in a function and then access modified value in the caller. The return type of my function is something else (boolean) and so I have to pass the int (or Integer) as method argument. But following code doesn't work:

      void bar ()
      Integer i = 2;

      foo (i);

      System.out.println (i); //still prints 2

      void foo (Integer i)
      i++; // even i = i + 1 won't work since i++ creates another object

      But, it doesn't work as Integer objects, I read, are immutable.

      Passing int as an argument will also not work.

      What is the way in JAVA to accomplish this ?

      Thanks in advance,

        1 2 Previous Next