Java tips --StaticUtility class modifier

Qualifier to StaticUtility class

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.

How to write a general Static Utility a little old

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";
  }
}

Recent writing style

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();
  }
}

Reason

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.

Conclusion

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

Java tips --StaticUtility class modifier
Java Tips
Access modifier [Java]
Java class methods
[Java] Class inheritance
Java code TIPS
java Scanner class
Java HashMap class
java (abstract class)
[Java] Nested class
Java anonymous class
About Java class
[java] abstract class
[Java] Object class
Java local class
java final modifier
Play Framework2.5 (Java) Tips
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
Java inner class review
Java class type field
Java programming (class method)
About Java String class
Java programming (class structure)
About java abstract class
[Java] Integer wrapper class reference
Java memo (standard class) substring
Java inflexible String class substring
Java memo (standard class) length
Java basic learning content 5 (modifier)
[Implementation] Java Process class notes
About Java class loader types
How to use java class
[Java] Comparator of Collection class
Java class definition and instantiation
Java learning memo (abstract class)
Java tips --Spring execution Summary
Summary of Java Math class
[Java] What is class inheritance?
About Java class variables class methods
[Java] Tips for writing source
[Java basics] What is Class?
Create an immutable class with JAVA
Why are class variables needed? [Java]
Call Kotlin's sealed class from Java
[Java] Object-oriented syntax --class method / argument
Various methods of Java String class
[Java] Memo for naming class names
How to disassemble Java class files
Kotlin Class to send to Java developers
Java High Level REST Client Tips
StringBuffer and StringBuilder Class in Java
How to decompile java class files
[Java] How to use LinkedHashMap class
java (use class type for field)
Java source code reading java.lang.Math class
[Java] Button template class by Graphics
Introducing Java tips GreenMail to Junit5
How to use class methods [Java]
[Java] How to use Math class