Possible Duplicate:
When should one use final?
When should Java programmers prefer to use
final Date now = new Date();
over
Date now = new Date();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Apart from deciding if a variable should be final or not which is covered in other posts, I think the problem with
final Date now = ..., is that although the reference to now will not change (it is final), its value might. So I find this a little misleading for developers who don’t know that Date is mutable.For example, you could write:
and get 2 different dates out of your final date…
Now this is true for any mutable variable (the content of
final List<String> lcan change too), but I think in the case of a Date, it is way too easy to assume immutability.A better solution would be using the joda time library:
in this case,
nowis immutable and won’t change (reference & value).