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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:20:42+00:00 2026-05-23T14:20:42+00:00

A recent question here had the following code (well, similar to this) to implement

  • 0

A recent question here had the following code (well, similar to this) to implement a singleton without synchronisation.

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

Now, I think understand what this is doing. Since the instance is static final, it’s built long before any threads will call getInstance() so there’s no real need for synchronisation.

Synchronisation would be needed only if two threads tried to call getInstance() at the same time (and that method did construction on first call rather than at "static final" time).

My question is therefore basically: why then would you ever prefer lazy construction of the singleton with something like:

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

My only thoughts were that using the static final method may introduce sequencing issue as in the C++ static initialisation order fiasco.

First off, does Java actually have this problem? I know order within a class is fully specified but does it somehow guarantee consistent order between classes (such as with a class loader)?

Secondly, if the order is consistent, why would the lazy construction option ever be advantageous?

  • 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-23T14:20:43+00:00Added an answer on May 23, 2026 at 2:20 pm

    Now, I think understand what this is doing. Since the instance is static final, it’s built long before any threads will call getInstance() so there’s no real need for synchronisation.

    Not quite. It is built when the SingletonHolder class is initialized which happens the first time getInstance is called. The classloader has a separate locking mechanism, but after a class is loaded, no further locking is needed so this scheme does just enough locking to prevent multiple instantiation.

    First off, does Java actually have this problem? I know order within a class is fully specified but does it somehow guarantee consistent order between classes (such as with a class loader)?

    Java does have a problem where a class initialization cycle can lead to some class observing another class’s static finals before they are initialized (technically before all static initializer blocks have run).

    Consider

    class A {
      static final int X = B.Y;
      // Call to Math.min defeats constant inlining
      static final int Y = Math.min(42, 43);
    }
    
    class B {
      static final int X = A.Y;
      static final int Y = Math.min(42, 43);
    }
    
    public class C {
      public static void main(String[] argv) {
        System.err.println("A.X=" + A.X + ", A.Y=" + A.Y);
        System.err.println("B.X=" + B.X + ", B.Y=" + B.Y);
      }
    }
    

    Running C prints

    A.X=42, A.Y=42
    B.X=0, B.Y=42
    

    But in the idiom you posted, there is no cycle between the helper and the singleton so there is no reason to prefer lazy initialization.

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

Sidebar

Related Questions

In a recent interview I had this question. Whats the error here? I know
A recent convert to Ruby here. The following question isn't really practical; it's more
Following the question I had asked yesterday which is here passing contents from multiple
A recent question here made use of the default keyword in non-generic code that
This recent question about sorting randomly using C# got me thinking about the way
Following on from my recent question on Large, Complex Objects as a Web Service
Following on from my recent question regarding parsing XML files in Java I have
In this answer to a recent question , I was advised to be wary
I recently asked a question (and had it answered) here: jQuery Load JSON I
Following the recent discussions here (e.g. 1 , 2 ) I am now using

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.