본문 바로가기

Develop/Java

@AllArgsConstructor와 @RequiredArgsConstructor 차이

728x90

@RequiredArgsConstructor 초기화 되지 않은 final 필드와 @NonNull 어노테이션이 붙은 필드에 대한 생성자를 생성합니다.

@AllArgsConstructor 모든 필드에 대한 생성자를 생성합니다.

 

@RequiredArgsConstructor


@RequiredArgsConstructor generates a constructor with 1 parameter for each field that requires special handling. All non-initialized final fields get a parameter, as well as any fields that are marked as @NonNull that aren't initialized where they are declared. For those fields marked with @NonNull, an explicit null check is also generated. The constructor will throw a NullPointerException if any of the parameters intended for the fields marked with @NonNull contain null. The order of the parameters match the order in which the fields appear in your class.

 

 

@AllArgsConstructor


@AllArgsConstructor generates a constructor with 1 parameter for each field in your class. Fields marked with @NonNull result in null checks on those parameters.

'Develop > Java' 카테고리의 다른 글

classpath* vs classpath 차이점  (0) 2023.03.21
자바 코딩 컨벤션 적용하기  (0) 2023.01.02
Lombok  (0) 2022.11.19
SOLID 원칙  (0) 2022.11.11
Java Exception  (1) 2022.09.22