Home >Java >javaTutorial >How to implement singleton mode in java enumeration
Usage instructions:
is similar in function to the public field. In addition to being more concise, it also comes with a serialization mechanism, and even in the face of complex serialization or reflection attacks, it also provides protection against multiple Explicit guarantee of instantiation. Although this approach is not yet widely adopted, single-element enumeration types are the best way to implement the Singleton pattern.
Usage example:
public enum Elvis { INSTANCE; private final String[] favoriteSongs = { "Hound Dog", "Heartbreak Hotel" }; public void printFavorites() { System.out.println(Arrays.toString(favoriteSongs)); } }
The above is the detailed content of How to implement singleton mode in java enumeration. For more information, please follow other related articles on the PHP Chinese website!