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

  • Home
  • SEARCH
  • 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 8340199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:06:54+00:00 2026-06-09T05:06:54+00:00

I know of 2 ways to implement the singleton pattern in java and im

  • 0

I know of 2 ways to implement the singleton pattern in java and im wondering which one is better and why.

the first way is:

  1. declare the constructor of the class private
  2. have everything inside the class static – basiclly have the class instance itself be the singleton

second way is:

  1. declare the constructor of the class private
  2. have a static member to hold the singleton (which may be an instance of the class)
  3. have a static getInstance() method

I tend to think that even though the second approach is the most common, the first approach may produce better code readability, both approaches seem similiarly efficient in runtime complexity, so i dont really get the reasons behind why the second approach is way more common and considered better practice…

enlighten me!

  • 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-06-09T05:06:55+00:00Added an answer on June 9, 2026 at 5:06 am

    The first approach is not a singleton. A singleton is a class of which precisely one instance, no more, no less, can exist. The first thing is sometimes called a “static class”, a “utility class,” or an “uninstantiable class.”

    There are a number of things that you can do with a “real” singleton that you can’t do with a utility class. For example, you can have a singleton that implements an interface or extends another class; you can’t do that with the all-static-methods thing. The all-static-methods class is generally evidence that no object oriented design analysis was done

    As far as how many ways there are to implement the singleton pattern in Java, there are actually quite a number of interesting ways, using different language features to defer initialization until absolutely needed: class loading, enumerations, or just a synchronized block and an if.

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

Sidebar

Related Questions

Want to know ways to store OBJECTS of a Class in some persistent storage.
Is there ways to implement model validation with complex rules like if one radio
I would like to know the best way to handle and implement session time
So, i found different ways to implement the creation of the singleton. EDIT: When
I'm looking for a better way to implement a decision tree in javascript. Being
One of the ways to implement Dependency Injection correctly is to separate object creation
I want to declare a variable which holds a class which implements a specific
I would like to have a class implement operator() several different ways based on
Does anyone know ways of partially or fully automating driver test installation? I am
I know two ways to check parameters of the method and throw exceptions when

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.