I want to force future users of a class to access a private member also from future code written in that class only through an accessor method (even through junit or anything like that).
is there a way to do it in java? can someone show an example if possible?
You cannot force to do that, but you can create a method and document that enforcement in the javadoc.
Also, there is an alternative solution which might work. Use ThreadLocal, like this:
The member field is final and cannot be changed. Therefore, clients will be forced to call the setter directly.