Skip to content

Instantly share code, notes, and snippets.

@cpilsworth
Created June 7, 2011 12:09
Show Gist options
  • Save cpilsworth/1012101 to your computer and use it in GitHub Desktop.
Save cpilsworth/1012101 to your computer and use it in GitHub Desktop.
The awesome power of nullify
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.*;
@RunWith(JUnit4.class)
public class TestNullify {
@Test
public void objectFailsToNullify() {
String x = "hello world";
assertEquals("hello world", x);
nullify(x);
assertEquals("hello world", x);
}
/**
* This method only nulls the reference that has been copied by value - as it exits
* obj will only be null in the scope of this method. The object that it references
* will retain its value.
public static void nullify(Object obj) {
if (obj != null) {
obj = null;
}
assertNull(obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment