Home > database >  Stop Lombok from setting default value for attributes
Stop Lombok from setting default value for attributes

Time:09-28

I have this Lombok class , when I am creating its object it is setting default values for attributes which is not being set. At places where I am not setting value for httpResponseCode , it is being set as 0. I don't want this variable to take default int value. Is there any way to stop that ?

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
@AllArgsConstructor
public class StatusInfo {
 public String reasonCode;
 public String result;
 public String description;
 public int httpResponseCode;
 }

CodePudding user response:

Its Java behavior. In Java, when an object is created, its properties are initialized with default values based on type:

boolean        # with value false
byte           # with value 0
short          # with value 0
char           # with value 0
int            # with value 0
long           # with value 0
float          # with value 0.0
double         # with value 0.0
class types    # with null

In Java arrays are also constructed by objects so arrays type parameters are also initialized with null when class object is created.

There is noting to do with Lombok.

CodePudding user response:

It is not related to Lombok. It is as per the default behaviour of primitive data types.

You can use wrapper classes instead of primitive data types.

For example : Integer for int,Double for double and likewise for other data types.You will get the default value as null instead of 0,0.0 respectively.

  • Related