6

Possible Duplicate:
When should one use final?

When should Java programmers prefer to use

final Date now = new Date();

over

Date now = new Date();
Community
  • 1
  • 1
Zango
  • 2,387
  • 3
  • 19
  • 33
  • It depends on how you want to use it. In most instances I really don't think it matters. – ahodder Apr 02 '12 at 16:22
  • 1
    Agree with aix - there's nothing about the use of `final` that is specific to `Date` variables. – Andrzej Doyle Apr 02 '12 at 17:13
  • I asked not so clearly what I was interested in... This http://stackoverflow.com/questions/266806/is-there-any-performance-reason-to-declare-method-parameters-final-in-java is what i wanted... – Zango Apr 03 '12 at 08:13

5 Answers5

8

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:

public static void main(String[] args) {
    final Date now = new Date();
    System.out.println(now);
    now.setHours(5);
    System.out.println(now);
}

and get 2 different dates out of your final date...

Now this is true for any mutable variable (the content of final List<String> l can 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:

final DateTime now = new DateTime();

in this case, now is immutable and won't change (reference & value).

assylias
  • 321,522
  • 82
  • 660
  • 783
3

In Java, a final variable is a variable whose value, once assigned, cannot be changed. You declare a variable final when that variable will be assigned a value, and you will never need to change that value.

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
2

When you use final keyword you can never change the value of variable.Same apply for date.

kundan bora
  • 3,821
  • 2
  • 20
  • 29
1

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to an different object.

However the data within the object can be changed. So the state of the object can be changed but not the reference.

With variables, the final modifier often is used with static to make the constant a class variable. Example:

class Test{
  final int value=10;
  // The following are examples of declaring constants:
  public static final int BOXWIDTH = 6;
  static final String TITLE = "Manager";

  public void changeValue(){
     value = 12; //will give an error
  }
}

if your requirement is of this type than you can use final with date.

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
1

If you declare a field, which is read-only, and you want to have a class thread-safe - then you should use the final modifier. Sometimes you're forced to make a variable final, if it's used in an anonymous class. In all other cases it doesn't really matter.

Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55