Created
September 21, 2015 17:24
-
-
Save smamran/0275f75b8f0e753ecdcc to your computer and use it in GitHub Desktop.
Java Object Reference Preservation
This file contains hidden or 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