Why Class.forName ("BumpTest") and not BumpTest.class?

In JLS Sec 8.4.3.6 synchronizedmethods , he says:

class BumpTest {
    // ...
    static synchronized void classBump() {
        classCount++;
    }
}

has the same effect as:

class BumpTest {
    // ...
    static void classBump() {
        try {
            synchronized (Class.forName("BumpTest")) {
                classCount++;
            }
        } catch (ClassNotFoundException e) {}
    }
}

It looks strange to me, not to mention overly complicated: why use Class.forName("BumpTest"), and not BumpTest.class? It is impossible that BumpTestit was not loaded, because it still executes code from this class. And writing it down as it is, the testee ClassNotFoundExceptionmust be caught and swallowed.

Is there a special reason to write it this way?

+6
source share
1 answer

, , . JLS 1.0 .

+3

All Articles