Is it mandatory utility class should be final and private constructor?

后端 未结 4 674
逝去的感伤
逝去的感伤 2021-02-02 00:55

By making private constructor, we can avoid instantiating class from anywhere outside. and by making class final, no other class can extend it. Why is it necessary for Util clas

4条回答
  •  天命终不由人
    2021-02-02 01:26

    It's not necessary, but it is convenient. A utility class is just a namespace holder of related functions and is not meant to be instantiated or subclassed. So preventing instantiation and extension sends a correct message to the user of the class.

提交回复
热议问题