본문 바로가기

- Clean Code

Non-thread-safe fields should not be static

반응형

 


제가 만든 유틸을 위와 같이 사용할 때 아래와 같은 에러가 발생했습니다.

Non-static method '' cannot be referenced from a static context

우선 원인은 static method는 그 class를 객체화하지 않아도 사용 가능해야하는데
해당 method에는 class를 객체화(인스턴스)해야 쓸 수 있는 method가 들어가면 안되기 때문입니다.

1차원 적으로 스태틱이 아니면 안된다니까 해당 메서드를 스태틱으로 변경했었습니다.
변경 후 이슈없이 잘 동작했습니다.

 

 


하지만... 소나큐브(클린코드 툴)에서 아래와 같이 스레드로부터 안전하지 않은 필드는 static으로 생성하지 않기를 권장했습니다.

 

우선 static 예약어를 제외하여 선언했습니다.

 

@Component로 Bean에 등록 후 사용했습니다.

 

 

 

 

반응형

'- Clean Code' 카테고리의 다른 글

String literals should not be duplicated  (0) 2020.07.15
Throwable.printStackTrace(...) should not be called  (0) 2020.07.15
DecimalFormat  (0) 2020.07.15