Should Helper/Utility Classes be abstract?

前端 未结 11 1962
时光说笑
时光说笑 2020-12-13 08:38

I commonly find myself extracting common behavior out of classes into helper/utility classes that contain nothing but a set of static methods. I\'ve often wondered if I sho

11条回答
  •  天命终不由人
    2020-12-13 09:11

    I don't declare utility classes abstract, I declare them final and make the constructor private. That way they can't be subclassed and they can't be instantiated.

    
    
    public final class Utility
    {
        private Utility(){}
    
        public static void doSomethingUseful()
        {
            ...
        }
    }
    

提交回复
热议问题