Created
August 9, 2016 14:07
-
-
Save rwheadon/b42154091549f0f912774a08817e7002 to your computer and use it in GitHub Desktop.
List Behavior that seems a bit non intuitive
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
/* | |
* in the simple test below I am a bit uncomfortable with how | |
* the private property pathSet is updated w/o a setter | |
* is this exploiting a Java bug or is it intentional and expected behavior | |
* that will be supported for a long time? | |
*/ | |
public class SimpleClass { | |
private List<String> pathSet; | |
public List<String> getPathSet() { | |
if(this.pathSet == null) { | |
this.pathSet = new ArrayList(); | |
} | |
return this.pathSet; | |
} | |
} | |
public class JavaScratchsheet{ | |
SimpleClass ss = new SimpleClass(); | |
ss.getPathSet().add("foo"); | |
System.out.println("pathSet = " + ss.getPathSet()); // >> pathSet = [foo] | |
ss.getPathSet().add("bar"); | |
System.out.println("pathSet = " + ss.getPathSet()); // >> pathSet = [foo, bar] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment