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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:56:40+00:00 2026-05-15T22:56:40+00:00

I just got to read the following code somewhere : public class SingletonObjectDemo {

  • 0

I just got to read the following code somewhere :

public class SingletonObjectDemo {

    private static SingletonObjectDemo singletonObject;
    // Note that the constructor is private
    private SingletonObjectDemo() {
        // Optional Code
    }
    public static SingletonObjectDemo getSingletonObject() {
        if (singletonObject == null) {
            singletonObject = new SingletonObjectDemo();
       }
       return singletonObject;
    }
}

I need to know what is the need of this part :

if (singletonObject == null) {
    singletonObject = new SingletonObjectDemo();
}

What if we do not use this part of code ? There would still be a single copy of SingletonObjectDemo, why do we need this code then ?

  • 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-15T22:56:41+00:00Added an answer on May 15, 2026 at 10:56 pm

    This class has a field SingletonObjectDemo singletonObject that holds the singleton instance. Now there are two possible strategies –

    1 – You do eager initialization of the object with the declaration –

    private static SingletonObjectDemo singletonObject = new SingletonObjectDemo();
    

    This will cause your singleton object to be initialized the moment the class is loaded. The downside of this strategy is that if you have many singletons, they will all be initialized and occupying memory even when they are not needed yet.

    2 – You do lazy initialization of the object i.e. initialize it when the first call to getSingletonObject() is made –

    // note that this initializes the object to null by default
    private static SingletonObjectDemo singletonObject;
    

    …

    if (singletonObject == null) {
            singletonObject = new SingletonObjectDemo();
        }
    

    This saves you the memory until the singleton is really needed. The downside of this strategy is that the first invocation of the method may see slightly worse response time as it will have to initialize the obejct before returning it.

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

Sidebar

Related Questions

I have got the following class generated from xsd.exe. Thats why i can't just
i just got a call from Hosting company, they claimed that my application creates
We just got our first major Perl CGI assignment in my CS class. Our
I got the following insistent JS issue just for IE 8-9, in other browsers
I just got done following the steps in this MSDN article on creating a
I'm using the following code to read from stdin int size; int bufferSize=2000; char
I am developing in python a file class that can read and write a
I used the following code: data<-read.csv('/Users/mf/Desktop/acceptor.csv') just_nums<-sapply(data,is.numeric) just_nums_data<-data[,just_nums] str(just_nums_data) heatmap(just_nums_data) But got this output:
I have a class with a member Address that I read from an XML
Just saw some interesting code while doing a typo in coffee-script. I got the

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.