String userInfo="zhaohui#25"; Prefer to write a class to describe this dataset UserInfo userInfo2=new UserInfo("zhaohui", 25); class UserInfo{ private String userName; private int age; public UserInfo(String userName,int age){ this.userName=userName; this.age=age; } //Set, get methods }
public class ThreadLocal{ private ThreadLocal(){} public static void set(String key,Object obj); public static void get(String key); } Problem: These strings represent a shared global namespace. For these methods to work, the string keys provided by the client must be unique. In fact, you can use an unforgeable key (sometimes called the ability key) to replace the string public class ThreadLocal{ private ThreadLocal(){} public static class Key{ Key(){} } public static Key getKey(){ return new Key(); } public static void set(Key key,Object value); public static Object get(Key key); }