//不可实例化类 public class DemoUtils { //防止实例化 private DemoUtils(){ throw new RuntimeException("不可实例化类"); } //... }
设计原因:
-
只包含静态域和静态方法,常常作为一些工具类,无需实例化,如Arrays和Collections。
-
可以把final类的方法组织起来(final类不能子类,利用它整合final类的方法生成新的功能)
-
Java8虽然提供了接口静态方法,但是接口中不可以有私有方法。有些复杂的功能不可能把所有代码都放在一个方法中。