variables, which represent storage locations in the computer's memory. The contents of these memory locations, at any given point in the program's execution, is called the program's state.
= 0; public void init() { orcsKilledYesterday = 1; orcsKilledToday = 2; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } Result should be 1
= 0; public void init() { orcsKilledToday = 2; orcsKilledYesterday = 1; } public int getDeadOrcs() { return orcsKilledYesterday + orcsKilledToday; } } Result in this case will be 2
List<String> books) { this.books = books; } public void addBooks(List<String> additionalBooks) { this.books.addAll(additionalBooks); } public List<String> getBooks() { return books; } } How do we make this immutable?
BookStore(final List<String> books) { this.books = books; } public BookStore addBooks(List<String> additionalBooks) { List<String> newBooks = getBooks(); newBooks.addAll(additionalBooks); return new BookStore(newBooks); } public List<String> getBooks() { List<String> copy = new ArrayList<>(); for (String book : books) { copy.add(book); } return copy; <- Return deep copy, not the original } }