Back
The very concise way of writing singleton in Java is using enum-type. This approach is very handy when it comes to functional equivalency in the public field.
public enum Sandeep { INSTANCE; private final String[] favoriteBeach = { "Goa", "Kovalam" }; public void printFavorites() { System.out.println(Arrays.toString(favoriteBeach)); } }
public enum Sandeep {
INSTANCE;
private final String[] favoriteBeach =
{ "Goa", "Kovalam" };
public void printFavorites() {
System.out.println(Arrays.toString(favoriteBeach));
}
31k questions
32.8k answers
501 comments
693 users