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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:29:18+00:00 2026-05-30T11:29:18+00:00

When a constructor uses a reference to a constant that is being lazily instantiated,

  • 0

When a constructor uses a reference to a constant that is being lazily instantiated, java throws an ExceptionInInitializerError (specifically on the line “this(ClassA.INSTANCE1)”).

public class ClassA {
    public static final ClassA INSTANCE1 = get("INSTANCE1");
    public static final ClassA INSTANCE2 = get("INSTANCE2");

    private static final Map<String, ClassA> MULTITON_MAP = new HashMap<String, ClassA>();

    private String name;

    private ClassA(String name) {
        this.name = name;
    }

    public static ClassA get(String name) {
        ClassA toReturn = MULTITON_MAP.get(name);
        if (toReturn == null) {
            toReturn = new ClassA(name);
            MULTITON_MAP.put(name, toReturn);
        }
        return toReturn;
    }
}

public class ClassB {
    private ClassA type;

    public ClassB() {
        this(ClassA.INSTANCE1);
    }

    public ClassB(ClassA type) {
        this.type = type;
    }

    public static void main(String[] args) {
        new ClassB();
    }
 }

I’ve solved the problem by removing the lazy instantiation and moving instantiations into a static block.

public class ClassA {
    public static final ClassA INSTANCE1;
    public static final ClassB INSTANCE2;

    ...

    static {
        INSTANCE1 = new ClassA("INSTANCE1");
        INSTANCE2 = new ClassA("INSTANCE2");

        MULTITON_MAP.put("INSTANCE1", INSTANCE1);
        MULTITON_MAP.put("INSTANCE2", INSTANCE2);
    }

    ...
}

So, my question is, why is Java unable to handle what I did previously. What causes the error and why?

Thanks a bunch!

  • 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-30T11:29:20+00:00Added an answer on May 30, 2026 at 11:29 am

    I think your problem is that you are trying to initialize INSTANCE1 before MULTITON_MAP, but the instantiation of INSTANCE1 depends on MULTITON_MAP.

    E.g. when you call MULTITON_MAP.get(name); in get, MULTITON_MAP is still null.

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

Sidebar

Related Questions

Is it good practice to have a class constructor that uses default parameters, or
I'm working on someone's code and they have a constructor that uses: class qwerty
I am rewriting a project so that it uses getters and setters to reference
I'm building an html page that uses backbone.js How can I override the constructor
I have a third party library that internally constructs and uses the SqlConnection class.
I'm dealing with a large code base that uses the following construct throughout class
I'm looking to reimplement some Tcl code that uses the socket -server construct [1].
I have a constructor like as follows: public Agent(){ this.name = John; this.id =
At the XmlSerializer constructor line the below causes an InvalidOperationException which also complains about
Socket has a constructor that takes a winsock SOCKET as parameter and stores it

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.