Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (6.1k points)
I want to know is there any optimized way to draw a singleton pattern in Java?

1 Answer

0 votes
by (11.7k points)

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)); 

}

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 25, 2021 in Java by rahulnayar01123 (6.1k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 13, 2019 in Java by Shubham (3.9k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...