你可以去了解一下Java单例模式,如果你无论什么时候都是在其他类去实例化Apple对象的话,很可能会引起内存的大量消耗。使用单例模式能避免内存浪费,同时又能达到获取实例化对象的目的。比如一个单例模式:
public class Singleton {
private Singleton();
private static class SingletonFactory {
private static Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonFactory.instance;
}
public Object readResolve() {
return getInstance();
}
}