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();
3
  • It depends on how you want to use it. In most instances I really don't think it matters. Commented Apr 2, 2012 at 16:22
  • 1
    Agree with aix - there's nothing about the use of final that is specific to Date variables. Commented Apr 2, 2012 at 17:13
  • I asked not so clearly what I was interested in... This stackoverflow.com/questions/266806/… is what i wanted... Commented Apr 3, 2012 at 8:13

5 Answers 5

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Those setter methods are deprecated, but you're right, they can be called. Yours is the only correct answer that I've seen so far. 1+
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.

1 Comment

Question is what would be the advantage, not what final does.
2

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

Comments

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.

1 Comment

+1 for the first 2 paragraphs :) .
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.