Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:47:23+00:00 2026-05-13T08:47:23+00:00

Can anyone provide an example of a singleton pattern and explain why they are

  • 0

Can anyone provide an example of a singleton pattern and explain why they are necessary?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:47:23+00:00Added an answer on May 13, 2026 at 8:47 am

    Before going the singleton route, reconsider. Do you really need a singleton? If you’re asking for scenarios when you need to implement singletons, it’s because the need for them didn’t really express. You’re better off not introducing singletons in your code base just because it feels cool to follow design patterns.

    Clean Code Talks – Global State and Singletons

    Once Is Not Enough

    Performant Singletons

    However, what’s really worth knowing is Dependency Injection.

    Now if you really want to implement singletons in Java, I would recommend Joshua Bloch’s “Effective Java” way of implementing them:

    public class Singleton
    {
      public static Singleton getInstance() {
        return SingletonHolder.instance;
      }
    
      private Singleton() {}
    
      private static final class SingletonHolder {
        static final Singleton instance = new Singleton();    
      }
    }
    

    The JLS guarantees the JVM will not initialize instance until someone calls getInstance();

    Final note, the Double Checked Locking Pattern is broken in Java up to Java 5. The Java 5 memory model makes the DCL pattern thread safe but it makes it slower than the SingletonHolder class method while the original intent was performance optimization.

    EDIT: As @Luno pointed out, since the second edition of the book, the preferred way is:

    As of release 1.5, there is a third approach to implementing singletons. Simply make an enum type with one element:

    // Enum singleton - the preferred approach
    public enum Elvis {
        INSTANCE;
    
        public void leaveTheBuilding() { ... }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.