[concurrency-interest] Joda-Time immutability

Stephen Colebourne scolebourne at joda.org
Mon Jun 20 10:03:09 EDT 2011


The "reflection can set final fields" comment was directed at
serialization, but has actually provided a reasonably acceptable
solution (thanks Mark).

Basically, I've made the fields final, and changed the setters to use
reflection (breaking the final restriction). Since 99% of use cases
are immutable, this will only impact the mutable users (a little bit
slower), and they should really move to the immutable classes
anyway...

I'd still like to know if my volatile write/read code posted earlier
would have been valid.

Stephen


On 20 June 2011 12:09, Mark Thornton <mthornton at optrak.com> wrote:
> On 20/06/11 11:33, Stephen Colebourne wrote:
>>
>> c) Try to do something clever with serialization to read the fields in
>> manually. No - can't then store the read data as the instance variable
>> has to be final...
>
> You can use reflection to write to final fields. This permitted under the
> memory model subject to certain restrictions.
>
> Mark Thornton
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>


More information about the Concurrency-interest mailing list