0

For example, if parameters are passed by value (even a reference) then how can we do something like this in java...

public void switchP(Pointer p1, Pointer p2){
p2 = p1;
}

And have it reflect back in main? Obviously this won't work because the p1 and p2 addresses are created on the stack and thus disappear when the method returns.

scarecrow-
  • 86
  • 1
  • 7
  • 3
    You can't pass arguments by reference in Java. – shmosel May 18 '16 at 18:33
  • In this case, you can probably pass in whatever contains the references to p1 and p2 and change them there. `public void switchP(Container c) { Pointer tmp=c.p1; c.p1=c.p2; c.p2=tmp; }` - Some code in Java is better written differently. – zapl May 18 '16 at 18:46

0 Answers0