Skip to content

Instantly share code, notes, and snippets.

@smamran
Created September 21, 2015 17:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save smamran/0275f75b8f0e753ecdcc to your computer and use it in GitHub Desktop.
Java Object Reference Preservation
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