Last active
November 25, 2018 18:59
-
-
Save Abhishek-Chaudhary-InfoCepts/dfd2c4f10b91407df6600ba0add1f234 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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