Skip to content

Instantly share code, notes, and snippets.

@genderquery
Created February 8, 2017 19:59
Show Gist options
  • Save genderquery/a1985f8b8dd4515b0a884e67b2782c53 to your computer and use it in GitHub Desktop.
Save genderquery/a1985f8b8dd4515b0a884e67b2782c53 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Main {
public static void main(String[] args) {
Household household = new Household();
final List<Person> people = household.getPeople();
// will throw UnsupportedOperationException
people.add(new Person("Avery Larsen"));
}
}
class Person {
private String name;
Person(final String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Household {
private List<Person> mPeople;
public Household() {
mPeople = new ArrayList<>();
}
public List<Person> getPeople() {
return Collections.unmodifiableList(mPeople);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment