I am working on Java + Spring Boot example and have below code snippets.
Case-1
public class EmployeeRowMapper implements RowMapper<Employee>{
@Override
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
return Employee.builder()
.firstName(rs.getString("FIRST_NAME"))
.lastName(rs.getString("LASTN_AME"))
.email(rs.getString("EMAIL"))
.address(rs.getInt("ADDRESS")).build();
}
}
Now, people (in code review comments) are suggesting me to create constants for FIRST_NAME, LAST_NAME, EMAIL and ADDRESS
like below which I feel is absolutely not needed and doesn't bring any usefulness as per my knowledge and creating these constants will consume 4 bytes of memory and reference to these variable will never be garbage collected.
This is simple use-case I've shown using small code snippet of Spring Batch code, but this approach people do use when creating Controllers, Services or DAO/Repository
layer in Java and where not.
Case-2
public class EmployeeRowMapper implements RowMapper<Employee>{
public static final String FIRST_NAME = "FIRST_NAME";
public static final String LAST_NAME = "LAST_NAME";
public static final String EMAIL = "EMAIL";
public static final String ADDRESS = "ADDRESS";
@Override
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
return Employee.builder()
.firstName(rs.getString(FIRST_NAME))
.lastName(rs.getString(LAST_NAME))
.email(rs.getString(EMAIL))
.address(rs.getInt(ADDRESS)).build();
}
}
Note: Here I really need to understand which Case (i.e., Case-1 or Case-2)
I need to follow and why ?
Also, people changing below controller to
@GetMapping(path = "/employee/{employeeId}")
public EmployeeDto getEmployee(@PathVariable Long employeeId) {
return employeeIdService.getTridById(employeeId);
}
To
public static final String EMP_GET = "/employee/{employeeId}";
@GetMapping(path = EMP_GET)
public EmployeeDto getEmployee(@PathVariable Long employeeId) {
return employeeIdService.getTridById(employeeId);
}
Say I've 100 Controllers and almost 500 endpoints in my modular application, so logically I am creating 500 String constants. Does this 500 constants doesnot occupy the memory area in the string constant pool?