---
title: "Singleton pattern in Java?"  
description: "Singleton pattern in Java?"  
author: "Anonymous User"  
published: 2015-07-17  
updated: 2015-07-18  
canonical: https://www.mindstick.com/forum/23350/singleton-pattern-in-java  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# Singleton pattern in Java?

What is an [efficient](https://www.mindstick.com/articles/33637/three-tips-for-an-energy-efficient-home-for-2019) way to implement a [singleton pattern](https://www.mindstick.com/forum/158344/how-do-you-implement-a-singleton-pattern-in-javascript) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Anonymous User

Use an enum:\

```
public enum Foo {    INSTANCE;}
```

```
public enum Elvis {    INSTANCE;    private final String[] favoriteSongs =        { "Hound Dog", "Heartbreak Hotel" };    public void printFavorites() {        System.out.println(Arrays.toString(favoriteSongs));    }}
```

\
"This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a [singleton](https://www.mindstick.com/blog/10912/java-singleton-pattern)."


---

Original Source: https://www.mindstick.com/forum/23350/singleton-pattern-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
