상세 컨텐츠

본문 제목

Utility classes should not have public constructors

꼬꼬마

by TISTORY 2017. 5. 23. 13:25

본문

Utility classes, which are a collection of static members, are not meant to be instantiated.

Even abstract utility classes, which can be extended, should not have public constructors.

Java adds an implicit public constructor to every class which does not define at least one explicitly.

Hence, at least one non-public constructor should be defined.

Noncompliant Code Example

class StringUtils { // Noncompliant
  public static String concatenate(String s1, String s2) {
    return s1 + s2;
  }
}

Compliant Solution

class StringUtils { // Compliant
  private StringUtils() {
    throw new IllegalAccessError("Utility class");
  }
  public static String concatenate(String s1, String s2) {
    return s1 + s2;
  }
}


'꼬꼬마' 카테고리의 다른 글

아두이노 동호회 가입  (0) 2017.06.08
postgresql - lead, lag over  (0) 2017.06.07
mybatis Error selecting key or setting result to parameter object  (0) 2017.03.15
sdfsdf  (0) 2017.01.05
https://www.keycdn.com/blog/front-end-frameworks  (0) 2016.12.23

관련글 더보기