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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:02:23+00:00 2026-05-25T20:02:23+00:00

On my last interview I was asked a standard question about all Singleton implementations

  • 0

On my last interview I was asked a standard question about all Singleton implementations in java. And how bad they all are.

And I was told that even the static initialization is bad because of the probability of unchecked exception in constructor:

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

    public static Singleton getInstance() {
        return instance;
    }
    private Singleton() {
       throw new RuntimeException("Wow... Exception in Singleton constructor...");
    }
}

And they also told me that the Exception is gonna be “ClassNotFoundException” so it would be extremely hard to find the problem in real app.

I tried to get that Exception:

public static void main(String[] args) {
    new Thread(new Runnable(){
            public void run() {
                Singleton.getInstance();
            }
        }).start();
}

But the onlything I get is ExceptionInInitializerError…

I googled about that exception and everywhere I found – they all talked about the same problem I was told on my interview. Nothing about the “implementation”=)

Thanks for your attention.

  • 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-25T20:02:24+00:00Added an answer on May 25, 2026 at 8:02 pm

    Sometimes you get strange questions in interviews, you should always expect the unexpected. 😉

    Any technique you use has positives and minuses. You have to know when is a good time to use them and what the problem might be and how to work around them.

    The important thing to remember that almost every problem has a solution or a work around.

    On my last interview I was asked a standard question about all Singleton implementations in java. And how bad they all are.

    Sounds less like a question, more like a religious debate.

    And I was told that even the static initialization is bad because of the probability of unchecked exception in constructor:

    Siderman IV: Spider man vs Singleton and the Static Initialiser. (the bad guys 😉

    throw new RuntimeException(“Wow… Exception in Singleton constructor…”);

    That is a bad idea, so don’t do that. In fact it won’t even compile. The compiler in Java 6 reports.

    initializer must be able to complete normally
    

    And they also told me that the Exception is gonna be “ClassNotFoundException” so it would be extremely hard to find the problem in real app.

    You get a ExceptionInInitializerError with a cause of the exception which caused the problem. Apart from having to read the nested exception its not that tricky.

    static class Inner {
        static {
            Integer.parseInt(null);
        }
    }
    
    public static void main(String... args) {
        try {
            new Inner();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        new Inner();
    }
    

    prints

    Exception in thread "main" java.lang.ExceptionInInitializerError
    at Main.main(Main.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:417)
    at java.lang.Integer.parseInt(Integer.java:499)
    at Main$Inner.<clinit>(Main.java:8)
    
    Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class Main$Inner
    at Main.main(Main.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    

    This happens the first time, after that you get NoClassDefFoundError if you try to use the class again.

    It used to be that when you tried to access a class using reflections, and it failed you got a ClassNotFoundException (with no details as to what really happens) However this is no longer the case.

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

Sidebar

Related Questions

Last day I faced an interview and they asked me java questions among which
Last week user Masse asked a question about recursively listing files in a directory
Last time I asked about the reverse process , and got some very efficient
A question I got on my last interview: Design a function f , such
I was asked this question in an interview. Although the interview was for dot
Last couple of months I've been wondering about all these password strength meters on
One of my former students sent me a message about this interview question he
Recently I had a question on the interview - I was asked to compare
I undertook an interview last week in which I learnt a few things about
Last night when I asked about screen scraping I was given an excellent article

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.