What is difference between in the following statements
String name = "Tiger";
final String name ="Tiger";
Although the String
class is final
class, why do we need to create a String "CONSTANT" variable as final?
What is difference between in the following statements
String name = "Tiger";
final String name ="Tiger";
Although the String
class is final
class, why do we need to create a String "CONSTANT" variable as final?
final
in this context means that the variable name
can only be assigned once. Assigning a different String
object to it again results in a compile error.
I think the source of the confusion here is that the final
keyword can be used in several different contexts:
See the Wikipedia article on final in Java for examples on each case.