Rachel
Rachel

Reputation: 103397

How to deal with Final Strings?

Is there any advantage of making String as final or can we make String as final ?, my understanding is that as String is immutable, there is no point of making it final, is this correct or they are situation where one would want to make String as Final ?

Code:

private final String a = "test";

or 

private String b = "test";

Upvotes: 6

Views: 20328

Answers (6)

fastcodejava
fastcodejava

Reputation: 41097

Modifier final means that variable cannot be changed.

final String s = "abcd";

// later in the code
s = "efgh";  // This will not compile

Immutable means that there is no such method available in the String class which allow to change its contents, e.g. setter, append, etc.

Upvotes: 1

Feras Odeh
Feras Odeh

Reputation: 9296

final means that the reference can never change. String immutability means something different; it means when a String is created (value, not reference, i.e: "text"), it can't be changed.

For example:

String x = "Strings Are ";
String s = x;

Now s and x both reference the same String. However:

x += " Immutable Objects!";
System.out.println("x = " + x);
System.out.println("s = " + s);

This will print:

x = Strings Are Immutable Objects
s = Strings Are

This proves that any String created cannot be changed, and when any change does happen, a new String gets created.

Now, for final, if we declare x as final and try to change its value, we'll get an exception:

final String x = "Strings Are ";
x += " Immutable Objects!";

Here is the exception:

java.lang.RuntimeException: Uncompilable source code - cannot assign a value to final variable x

Upvotes: 11

Peter Lawrey
Peter Lawrey

Reputation: 533492

One difference in having a final field, is that because String is immutable, it may as well be static.

private final String a = "test";

slightly more efficient, possibly clearer as

private static final String a = "test";

Upvotes: 0

Costis Aivalis
Costis Aivalis

Reputation: 13728

All fine here, but there are two more ways to "deal with Final Strings", as you put it, but in terms of 'where to place them':

  1. Create an interface that includes final static variables (and final Strings) and implement it wherever you need them, and
  2. Create a class for constants and make a static import.

Way 1, was popular some 10 years ago, and may still be where you have large collections of final variables. Way 2, is simple to use but a little weird, since you can refer to constants without qualification, which makes your code difficult to comprehend.

Upvotes: 0

helloworld922
helloworld922

Reputation: 10939

final means that the reference cannot change, not the value. Regardless of the final, Strings are always immutable.

final String a = "test";
String b = "hello";

a = "world"; // error, a can't change reference to the String object "world"
b = "two"; // ok, b points to a different String object "two"

Upvotes: 4

david van brink
david van brink

Reputation: 3642

It means that that variable can never even point to a different string.

(And yes, a String object is immutable... but a variable could point to a different instance of String. Unless it's final.)

(edit -- in your example, you didnt name your variable...

final String x = "test";

)

Upvotes: 4

Related Questions