Java: How to throw Legen ... dary ClassNotFoundException in Singleton?

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

And I was told that even static initialization is bad because of the likelihood of an uncontrolled exception in the 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 would be a "ClassNotFoundException", so it would be very difficult to find the problem in a real application.

I tried to get this exception:

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

But all I get is an ExceptionInInitializerError ...

I searched for this exception everywhere I found it — they all spoke of the same problem that they told me in my interview. Nothing about "implementation" =)

Thank you for the attention.

+5
4

, .;)

. , , .

, .

Singleton java. .

, , .

, - :

IV: - . ( ;)

throw new RuntimeException ( "Wow... Exception in Singleton constructor..." );

, . . Java 6 .

initializer must be able to complete normally

, Exception "ClassNotFoundException", .

ExceptionInInitializerError , . , .

static class Inner {
    static {
        Integer.parseInt(null);
    }
}

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

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)

, NoClassDefFoundError, .

, , , ClassNotFoundException ( - , ). .

+5

, , - ClassNotFoundException . , . ExceptionInInitializerError , Javadoc:

ExceptionInInitializerError ", . ExceptionInInitializerError - , .", .

: singleton Java - , ( Java API " Java" ) Singleton Enum Pattern (http://stackoverflow.com)/questions/70689/ , ----Java).

, , , , Singleton Java.

+3

, - , RuntimeException ctr Singleton, a ClassNotFoundException - - CNFEx - - , ctr , CNFEx.

0

. . log4j. log4j.jar classpath, . ClassDefNotFoundError. ClassNotFoundException, , : Class.forName("MyNotExistingClass")

In any case, I think your interviewer is wrong. A problem with static initialization is not possible. RunTimeExceptions: you will see them on your application's STDERR. The problem is that they are started before real objects are needed, and the lazy initialization of the classic singleton is only performed when the instance is actually used.

0
source

All Articles