Java Reference
In-Depth Information
/**
* Accessor for the statistics class. Only allows for
one instance of the
* class to be created.
* @return
*/
public static Statistics getInstance(){
return instance;
}
/**
* @return the teams
*/
public List getTeams() {
return teams;
}
/**
* @param teams the teams to set
*/
public void setTeams(List teams) {
this.teams = teams;
}
protected Object readResolve(){
return instance;
}
}
If another class attempts to create an instance of this class, it will use the getIn-
stance() accessor method to obtain the Singleton instance. It is important to note
that the solution code demonstrates eager instantiation, which means that the instance
will be instantiated when the Singleton is loaded. For lazy instantiation, which will be
instantiated upon the first request, you must take care to synchronize the getIn-
stance() method to make it thread-safe. The following code demonstrates an ex-
ample of lazy instantiation:
Search WWH ::




Custom Search