Do I have to declare it final? Why is that?
Not necessarily. But it can also be final, and preferably final, because ThreadLocal itself is only assigned once — but the data ina ThreadLocal changes.
The JDK API is private static. ThreadLocal is static because it belongs to the current requesting thread, that is, to the class, not to the object.
The life cycle
Each requesting thread has its own data
Typically, where the request comes in, data is written to the thread context. The data can then be read by the entire subsequent link of the current requesting thread.
Data must be deleted when the current thread of request terminates
Why delete it? Avoid memory leaks? Yes. If not deleted, the current requesting thread may accumulate more data and eventually cause a memory leak. So with ThreadLocal, data must be deleted when the current thread of request terminates.
Why don’t you delete it? Because the current requesting thread is reused by the Thread pool of the Web server, if the data of the current requesting thread is not deleted each time, the data of the thread may increase.
How to delete? The remove method.