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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:59:34+00:00 2026-06-09T13:59:34+00:00

(From Wikipedia) //Lazy initialization: public class Singleton { private static volatile Singleton instance =

  • 0

(From Wikipedia)

//Lazy initialization:

public class Singleton {

    private static volatile Singleton instance = null;

    private Singleton() {   }

    public static Singleton getInstance() {
            if (instance == null) {
                    synchronized (Singleton.class){
                            if (instance == null) {
                                    instance = new Singleton();
                            }
                  }
            }
            return instance;
    }
}

//Eager initialization:

public class Singleton {
   private static final Singleton instance = new Singleton();

   private Singleton() {}

   public static Singleton getInstance() {
      return instance;
   }
}

“If the program uses the class, but perhaps not the singleton instance itself, then you may want to switch to lazy initialization.”

1 – Not sure that I got it. Why should a program not use a class? Why can’t I solve it by just adding attributes/methods to it? How should the constant reference change in respect to that?

2 – In lazy initialization – How would Synchronizing getInstance() rather than the code block (getting rid of the double check), influence my program, assuming multithreading occures?

Thank you.

  • 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-09T13:59:35+00:00Added an answer on June 9, 2026 at 1:59 pm

    1 – Your application may include various jars, but you won’t necessarily use all of the functionality within each jar. Further, the users of your application may not use all of the functionality you’ve built into it. If you have a ReportABug class for example, they may never activate that functionality from the menu because you’re such an awesome programmer 😉

    Static fields/methods aren’t instantiated/invoked until the class is required at runtime, so your ReportABug class could have 1000’s of static fields, but fingers crossed, they’ll never consume any memory because the class is never loaded.

    Their point is that the class that has the singleton (or more than one) may have other static methods that don’t access the singleton.

    2 – you could synchronise the method against the Singleton class object, but every call to the getInstance would incur the overhead, it’s much cheaper just to check if instance == null (it’s one machine cycle). It is possible that another thread could preempt your thread and create the instance after the outer ==null check, so both tests are required.

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

Sidebar

Related Questions

Quote from Wikipedia : A public key token. This is a 64-bit hash of
From wikipedia: // A class template to express an equality comparison interface. template<typename T>
This is the paragraph on OpenID security from Wikipedia . Are there any new
Consider the following product and AbstractBuilder from wikipedia: /** Product */ class Pizza {
Lifting this example from Wikipedia : // the Window interface interface Window { public
From Wikipedia : Single responsibility principle states that every class should have a single
From wikipedia : During each sprint, typically a two to four week period (with
From Wikipedia: The complexity of the algorithm is O(n(logn)(loglogn)) bit operations. How do you
From wikipedia: The Web Services Description Language (WSDL, pronounced 'wiz-dəl') is an XML-based language
I know from wikipedia for example that exception handling is used in an application

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.