Java Object Reference Preservation
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
class ReferencePreservation { | |
public static void main(String[] args) { | |
B b = new B(); | |
b.x = 10; | |
System.out.println("Before method call b.x " + b.x); | |
A a = new A(); | |
a.method(b); | |
System.out.println("After method call b.x " + b.x); | |
} | |
} | |
class B { | |
int x; | |
} | |
class A { | |
public void method(B obj) { | |
System.out.println("Inside method call before modification obj.x " + obj.x); | |
obj.x = 300; | |
System.out.println("Inside method call before modification obj.x " + obj.x); | |
obj = null; | |
System.out.println("After putting null obj " + obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment