Persistence provider for Java that supports final fields

前端 未结 5 992
梦如初夏
梦如初夏 2021-01-12 00:52

I\'m very new to Java but I\'ve been developing a habit to use final wherever possible declaring immutability which i think is a good thing. (Consider f#)

I\'ve read

5条回答
  •  孤独总比滥情好
    2021-01-12 01:30

    This may not be exactly what you're striving for, but the immutability principle can easily be supported by non-final private fields having only a getter. The compiler in fact recognizes this and generates code which is pretty much identical to what you'd get with fields declared as final.

    This will require you to be conscientious and not change your fields from within the containing class (whereas final would enforce that), but I think this is not a big price to pay for the flexibility you gain.

提交回复
热议问题