Created
June 7, 2011 12:09
-
-
Save cpilsworth/1012101 to your computer and use it in GitHub Desktop.
The awesome power of nullify
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
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