Lazy initialization
get
and set
methods. This is called self-encapsulation.
Self-encapsulation:
get
and set
methods,
and provide their own definition of how fields behavepublic final class Infant { public Infant (final String name, final int weight, final String mood){ setName(name); setWeight(weight); setMood(mood); } public void becomeHungry(){ //instead of fMood = "Cranky", use: setMood("Cranky"); } public String toString(){ //use get methods instead of direct access final StringBuilder result = new StringBuilder(); result.append("Infant - Name: + "); result.append(getName()); result.append(" Weight: "); result.append(getWeight()); result.append(" Mood: "); result.append(getMood()); return result.toString(); } public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getWeight(){ return weight; } public void setWeight(final int weight){ this.weight = weight; } public String getMood(){ return mood; } public void setMood(final String mood){ this.mood = mood; } //..other methods elided // PRIVATE private String name; private int weight; private String mood; }