Well, as disturbing as it might be, I
am not afraid to at least admit it: I did not know about the elegance of
the following construct:
Since you are using an Enum, nobody can create new members from outside, you don't have to care about privatizing any methods and you can access it as easy as:
Isn't that fancy? I had to admit I never thought of it myself. A colleague of mine had to put it under my nose.
public enum TLData { INSTANCE; private final ThreadLocal<String> myData = new ThreadLocal<String>(); public String getMyData() { return myData.get(); } public void setMyData(final String myData) { this.myData.set(myData); } }
Since you are using an Enum, nobody can create new members from outside, you don't have to care about privatizing any methods and you can access it as easy as:
public class myClass { public void myMethod() { /** * setting the data */ TLData.INSTANCE.setMyData("some data"); /** * getting the data */ String myData = TLData.INSTANCE.getMyData(); } }
Isn't that fancy? I had to admit I never thought of it myself. A colleague of mine had to put it under my nose.
Kommentare