Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abhishek-Chaudhary-InfoCepts/dfd2c4f10b91407df6600ba0add1f234 to your computer and use it in GitHub Desktop.
Save Abhishek-Chaudhary-InfoCepts/dfd2c4f10b91407df6600ba0add1f234 to your computer and use it in GitHub Desktop.
package com.abhishek.dojo.guess.output;
public class ChangeObjectReferenceInsideMethod {
public class A {
int attr;
public void setAttr(int a) {
this.attr = a;
}
}
public static void main(String[] args) {
A a = new ChangeObjectReferenceInsideMethod().new A();
a.setAttr(1);
foo(a);
System.out.println(a.attr);
}
public static void foo(A t) {
t = new ChangeObjectReferenceInsideMethod().new A();
t.setAttr(2);
}
// ANS- 1
// Here even if object reference is passed to method foo ↵
// foo is changing the reference and once foos callstack is over ↵
// main will have original reference and not the modified reference
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment