Skip to content

Instantly share code, notes, and snippets.

@rwheadon
Created August 9, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwheadon/b42154091549f0f912774a08817e7002 to your computer and use it in GitHub Desktop.
Save rwheadon/b42154091549f0f912774a08817e7002 to your computer and use it in GitHub Desktop.
List Behavior that seems a bit non intuitive
/*
* 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