Java – there are only five instances of classes

I want only five instances of classes throughout the application lifecycle How can I do this? If possible, please provide sample code

Solution

Since singles should use enumeration (see "valid Java"):

public enum FiveInstance {

  INSTANCE1,INSTANCE2,INSTANCE3,INSTANCE4,INSTANCE5;

  public void anyMethod() {}

}

Gretz ghad

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>