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 8471783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:53:25+00:00 2026-06-10T16:53:25+00:00

I was looking at these two implementations of the Singleton Pattern: Class Customer {

  • 0

I was looking at these two implementations of the Singleton Pattern:

 Class Customer {

int x;
static int count = 0;

private Customer()
{
    x = 1;
}

public static Customer GetCustomer()
{
    if(count==0)
    {
        count++;
        return new Customer();
    }
    else
    {
        return null;
    }
}
}

Implementation 1 in which the constructor is not called if the class is already instantiated once. OR

 Class Customer{

int x;
static int count = 0;
public Customer()
{
    if(count == 0)
    {
        x = 1;
        count++;
    }
    else
    {
        return;
    }
}

Implementation 2 in which the constructor is called irrespective of weather the class is already instantiated once or not. I watched a video lecture online that says Implementation 2 is not preferred as it allocates memory for the constructor although the object is not instantiated for the second time. I am aware Java has automatic Garbage Collection, but just wanted to know if what I watched in the video lecture is relevant.

  • 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-10T16:53:26+00:00Added an answer on June 10, 2026 at 4:53 pm

    There are some that would say that no implementation of a singleton is the correct one, but I’m not quite in that camp.

    People often tend to use them badly (as a God object, for example) but they still have their place (in my opinion, which has little to do with this answer).

    For the purposes of this answer, I’m going to assume you’ve made the right decision in needing a singleton, but I do urge you to read up on its potential problems – there may well be a better way to achieve your ends.


    Having said that, I’m not sure if your code samples are correct anyway. A singleton is supposed to return one and only one instance, creating one if necessary, giving you the previously created one if not.

    I fail to see how your first code sample honors that contract. It’ll give you a new one the first time it’s called, then it will give you nothing after that.

    And your second code sample doesn’t seem to prevent multiple objects at all.

    So I’d be thinking very carefully about whether you want to continue watching that video, if this is the quality of the education they deliver.


    In any case, I prefer the one that only constructs once, since you’re only supposed to have one object of that class. In other words, a static getMe() call that is properly synchronised to prevent race conditions allowing creation of more than one Me object, and that creates a new object the first time, returning that same object on subsequent calls.

    In pseudo-code, that would be something like:

    class Me {
        private Me() {};
    
        private static Me *loner = NULL;
    
        public static synchronised Me *getMe() {
            if loner == NULL
                loner = new Me();
            return loner;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following two interfaces (these are small examples, not my actual implementation): public
I'm putting together my first web page, and I've been looking at these FTP
Im looking to make my own implementation of these Identicons or Gravatars found here
I'm looking for a tutorial explaining how to work with these 3 technologies, found
I'm looking for a Vim plugin that can do these kind of thing. Let's
I am looking for something very close to an application server with these features:
I was recently looking to practice utilizing dictionaries in python and stumbled across these
Looking for sql to get distinct names and count of those names from a
I'm looking for an elegant pattern to solve this problem: I have several user
Actually, this question seems to have two parts: How to implement pattern matching? How

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.