I am reading the book Effective
Java, written by Joshua Bloch. Now, I'm with Item 32: Avoid strings where other types are more appropriate.
I can't understand the last point: Strings are poor substitutes for capabilities. I copied the part that I dun understand:
----------------------------------------------------------------------
Occasionally, strings are used to grant access to some functionality. For example, consider the design of a thread-local variable facility. Such a facility provides variables for which each
thread has its own value. When confronted with designing such a facility several years ago, several people independently came up with the same design in which client-provided
string keys grant access to the contents of a threadlocal variable:
The problem with this approach is that the keys represent a shared global namespace. If two independent clients of the package decide to use the same name for their thread-local variable, they unintentionally share the variable, which will generally cause both clients to fail. Also, the security is poor; a malicious client could intentionally use the same key as another client to gain illicit access to the other client's data. This API can be fixed by replacing the string with an unforgeable key (sometimes called a capability):
While this solves both of the problems with the string-based API, you can do better. You don't really need the static methods any more. They can instead become instance methods on the key, at which point the key is no longer a key: it is a thread-local variable. At this point, the noninstantiable top-level class isn't doing anything for you any more, so you might as well get rid of it and rename the nested class to ThreadLocal:
This is, roughly speaking, the API that java.util.ThreadLocal provides. In addition to solving the problems with the string-based API, it's faster and more elegant than either of the key-based APIs.
----------------------------------------------------------------------
I really dun understand what he wants to say, especially with the ThreadLocal sample. Can any one tell me what does "Strings are poor substitutes for capabilities" actually mean? in any easier sample codes?
Thank you very much.