final int receiverUserId = sendMessageRequest.getReceiverUserId();
final int senderUserId = sendMessageRequest.getSenderUserId();
final int replyTo = sendMessageRequest.getReplyTo();
final int subsiteId = sendMessageRequest.getRelatedSubsiteId();
final String message = sendMessageRequest.getMessage();
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplateObject.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, senderUserId);
ps.setInt(2, receiverUserId);
ps.setInt(3, replyTo);
ps.setInt(4, subsiteId);
ps.setString(5, message);
return ps;
}
},
keyHolder
);
return true;
In the code above, if I remove final from one of the variables that I use to set PreparedStatement
, it says "" is accessed from within inner class, needs to be declared final
.
I understand java warns me to declare final, so I cannot change the variable later and break PreparedStatementCreater of which I just made an instance. I have read similar topics in stackoverflow, however I still cant understand it. I checked the function ps.setInt
, its arguments are of type int
. So they are passed by value. There is just no way I can change the value of them anyway. I really dont understand why I am getting warned here. Can you help me understand ?