when we write String str="mac";
and again we can initialize with str="joe";
why this happens when String is immutable where we can initialize only once for immutable object
Asked
Active
Viewed 58 times
-2

Subhrajyoti Majumder
- 40,646
- 13
- 77
- 103
-
Somewhat related - http://stackoverflow.com/q/279507/1679863 – Rohit Jain Feb 24 '14 at 05:19
-
declare string as final static – Peshal Feb 24 '14 at 05:20
-
you have misunderstood initialize and immutable – lakshman Feb 24 '14 at 05:20
-
1@Peshal Why `static`? Just `final` would be fine. But that isn't the question. – Rohit Jain Feb 24 '14 at 05:20
-
@Rohit how do you declare the constants? – Peshal Feb 24 '14 at 05:22
-
@Peshal Why do you need a constant? – Rohit Jain Feb 24 '14 at 05:24
-
1Why do people downvote this question? Everybody was begginer once. – Leos Literak Feb 24 '14 at 05:24
-
@LeosLiterak Because looking through tutorials and/or searching Google or Stack Overflow would have found easy answers. – chrylis -cautiouslyoptimistic- Feb 24 '14 at 05:45
-
@chrylis yes, but downvoting is kind of rude, if he tries – Leos Literak Feb 24 '14 at 06:19
1 Answers
2
String object is immutable, so you cannot modify its content. But you can reassign string variable to different String object.

Leos Literak
- 8,805
- 19
- 81
- 156