Should I use the "final" modifier when creating Date objects?

Possible duplicate:
When should the final version be used?

When Java programmers prefer to use

final Date now = new Date();

over

Date now = new Date();
+5
source share
5 answers

In addition to deciding whether the variable should be final or not, which is considered in other posts, I think the problem with final Date now = ...is that although the reference to the present does not change (it is final), its value may be. Therefore, I believe that this is a little misleading for developers who do not know that Date has changed. For example, you can 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 two different dates from your final date ...

( final List<String> l ), , .

joda:

final DateTime now = new DateTime();

now ( ).

+7

Java final - , . final, , .

+3

final, . .

+2

. , , .

. , , .

, , . :

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
  }
}

, final .

+1

If you declare a read-only field and you want the class to be thread safe, then you should use a modifier final. Sometimes you are forced to make a final variable if it is used in an anonymous class. In all other cases, this does not matter much.

+1
source

All Articles