Force non-instantiation with Effective Java private constructor

Effective Java's own interpretation. I tried to interpret item 4 of the 3rd edition by writing my own code.

Conclusion

For classes that have only static members, provide a private constructor that raises a runtime exception.

Description

For example, a utility class that collects only common processes (hereinafter referred to as Util class) has only static methods and static variables, and is not premised on instantiation.

In general, public Util class has a public default constructor unless a constructor is defined, so there is a concern that it will be unnecessarily instantiated in an external class.

By explicitly defining a private constructor, you can prevent constructor calls outside the Util class. Also, by putting exception output processing in the constructor, it is possible to make the constructor call in the Util class an error.

Code without constructor definition (bad example)

** Util class **

public class SampleUtil {

    public static final String HOGE = "hoge";

    public static String fugaMethod() {
        return "fuga";
    }
}

** Caller class **

public class SampleService {

    public String sampleMethod() {
        //Uselessly instantiated
        SampleUtil sampleUtil = new SampleUtil();
        return sampleUtil.fugaMethod();
    }
}

I am creating an instance in the calling class. Since fugaMethod () is a static method, it is useless to instantiate it.

Code with private constructor definition (good example)

** Util class **

public class SampleUtil {

    //Define a constructor that is private and outputs a runtime exception
    private SampleUtil() {
        throw new IllegalStateException("Util instance construction is not allowed.");
    }

    public static final String HOGE = "hoge";

    public static String fugaMethod() {
        return "fuga";
    }

}

** Caller class **

public class SampleService {

    public String sampleMethod() {
        return SampleUtil.fugaMethod();
    }
}

Since the constructor is private, it cannot be instantiated in another class in the first place.

Also, even if the constructor is called in the Util class, an exception will be output and no instance will be created. If you have code that calls the constructor, it's a programming error, so be sure to throw a runtime exception.

Recommended Posts

Force non-instantiation with Effective Java private constructor
[Read Effective Java] Chapter 2 Item 4 "Force uninstantiation with a private constructor"
[Read Effective Java] Chapter 2 Item 3 "Force singleton characteristics with private constructor or enum type"
[Java] Test private methods with JUnit
java (constructor)
Java constructor
Effective Java Chapter 2
Effective Java Chapter 6 34-35
Effective Java Chapter 4 15-22
Understand java constructor
Effective Java Chapter 3
[Java] How to omit spring constructor injection with Lombok
[Java] Refer to and set private variables with reflection
Install java with Homebrew
Change seats with java
Install Java with Ansible
effective java 3rd summary
Comfortable download with JAVA
Java paid private memo
Switch java with direnv
Studying Java 8 (see constructor)
Download Java with Ansible
JAVA constructor call processing
Let's scrape with Java! !!
Build Java with Wercker
Java encapsulation private public
Builder pattern (Effective Java)
Endian conversion with JAVA
[Read Effective Java] Chapter 2 Item 2 "Consider a builder when faced with a large number of constructor parameters"