如果我們希望傳遞給方法的參數不被修改而導致不必要的性能浪費。則可以在參數中加上
final
關鍵字
@Data
class Person {
private String name;
private Integer age;
}
public void method(final Person person){
// ...
}
上面代碼中的參數加上Connection屬性后,就不可以對conn進行修改了。但是可以對conn中的屬性修改
錯誤示例:person = new Person();
正確示例:person.setAge(23);