Occasionally you'll want to write a class that is just a grouping of static methods and static fields like java.util.Collections. Such utility classes were not designed to be instantiated, an instance would be nonsensical. A class can be made noninstantiable by including a private constructor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Noninstantiable utility class | |
public class UtilityClass { | |
// Suppress default constructor for noninstantiability | |
private UtilityClass() { | |
throw new AssertionError(); | |
} | |
... // Remainder omitted | |
} |
No comments:
Post a Comment