java - Why some programmers like to add final keyword in the method params? -
recently,i found in projects add final keyword in method parameters like:
public static string getsuffix(final string filename) { if (filename.indexof('.') >= 0) { return filename.substring(filename.lastindexof('.')); } return emtpy_string; } public httpresult(final int statuscode) { this.statuscode = statuscode; }
it can catch bugs.
for example, if write :
public httpresult(final int statuscode) { statuscode = statuscode; }
you compilation error, since assigning value local final variable, while if write
public httpresult(int statuscode) { statuscode = statuscode; }
you won't compilation error, statuscode
member won't assigned.
Comments
Post a Comment