Does it make sense to define a final String in Java? [duplicate]

The String object is immutable but what it is is actually a reference to a String object which could be changed.

For example:

String someString = "Lala";

You can reassign the value held by this variable (to make it reference a different string):

someString = "asdf";

However, with this:

final String someString = "Lala";

Then the above reassignment would not be possible and would result in a compile-time error.

Leave a Comment