e.g:
Normal :
String lc = "lowercase";Now it may look immutable.But you can modify it with the following snippet:
lc.toUpperCase();
System.out.println(lc);
Field stringValue = String.class.getDeclaredField("value");
stringValue.setAccessible(true);
stringValue.set(orig, orig.toUpperCase().toCharArray());
This is because of SecurityManager is not turned on.Though
you have an immutable class it is mutable unless the security manager
is turned on.
@anai. Your post was very informative. It would be more interesting if you would just give us a usecase where immutability is really important. In real world usecases, i think the final access qualifier should be able to staisfy our need since we will be abstracting already abstract objects. So can you give us a use case where immutability will be really put into use
ReplyDeleteN hw do i turn on the security manager??
ReplyDelete