Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java.util.Objects private constructor throws assertionError

Tags:

java

I have observed that java.util.Objects has a constructor which is throwing AssertionError.

 * @since 1.7
 */
public final class Objects {
    private Objects() {
        throw new AssertionError("No java.util.Objects instances for you!");
    }
 ...

This is a static utility class and hence no instance needed.

One possible reason I understand is, the developer is trying to ensure that no instance of this class is created. As the only way anyone can call this constructor is by reflection.

Is there any other reason to have this kind of constructor?

like image 221
Maas Avatar asked Sep 04 '14 06:09

Maas


People also ask

What causes Java Lang AssertionError?

An assertion is achieved using the assert statement in Java. While executing assertion, it is believed to be true. If it fails, JVM throws an error named AssertionError.

Can we use throws keyword with private constructor?

Yes, just like methods you can throw exceptions from constructors in. But, if you do so, you need to catch/throw (handle) the exception at the method where you invoke the constructor. If you don't a compile time error is generated.

What is Java Lang AssertionError?

The meaning of an AssertionError is that something happened that the developer thought was impossible to happen. So if an AssertionError is ever thrown, it is a clear sign of a programming error.

Why Utility classes should not have public constructors?

Utility classes, which are a collection of static members, are not meant to be instantiated. They should therefore not have public constructors. Java adds an implicit public constructor to every class which does not define at least one explicitly. Hence, at least one non-public constructor should be defined.


1 Answers

The sole purpose is to enforce noninstantiability. Since it is private, the AssertionError is mainly for reflection and for the class itself because private constructors can be called from the class itself. And as a side effect, this idiom also prevents the class from being subclassed.

Quoting from Effective Java 2nd Edition Item 4:

// Noninstantiable utility class
public class UtilityClass {
    // Suppress default constructor for noninstantiability
    private UtilityClass() {
        throw new AssertionError();
    }
    ... // Remainder omitted
}

Because the explicit constructor is private, it is inaccessible outside of the class. The AssertionError isn’t strictly required, but it provides insurance in case the constructor is accidentally invoked from within the class. It guarantees that the class will never be instantiated under any circumstances. This idiom is mildly counterintuitive, as the constructor is provided expressly so that it cannot be invoked. It is therefore wise to include a comment, as shown above. As a side effect, this idiom also prevents the class from being subclassed. All constructors must invoke a superclass constructor, explicitly or implicitly, and a subclass would have no accessible superclass constructor to invoke.

Also you might find this Question useful:

What is the preferred Throwable to use in a private utility class constructor?

like image 150
icza Avatar answered Jan 01 '23 21:01

icza