What qualifiers do you have for the StaticUtility class? I was wearing final without any doubt. Recent source code has abstract without final, so I will leave the details and explanation as a memorandum.
In the previous project, when creating the StaticUtility class, add the final modifier I was creating a private constructor so that it wouldn't be instantiated. (Every time, thinking that it's annoying)
//Add final to class modifiers to prevent inheritance
public final class A {
//Create a private construct so that it will not be instantiated
private A(){
}
public static String getHello() {
return "Hello";
}
}
I discovered it while reading Spring Framework classes, but recent strong people It seemed that I was creating a Static Utility with abstract. Spring Source
//I made it abstract and did not write the constructor
public abstract class FreeMarkerTemplateUtils {
public static String processTemplateIntoString(Template template, Object model)
throws IOException, TemplateException {
StringWriter result = new StringWriter();
template.process(model, result);
return result.toString();
}
}
Instances cannot be created for abstract classes. So you don't have to define a private constructor Since there is no need to worry about instantiation, I guess it will reduce the amount of coding.
Isn't it inherited if it is abstaract? I thought, but I can't override it when it's inherited, Above all, considering the time and effort of the private constructor, I thought that abstarct was overwhelmingly more advantageous. (In the first place, there are times when I forget to write the privatee constructor, so it also prevents that!)
Let's imitate the source code of stronger and stronger people!
Recommended Posts