Immutable Type: public final fields vs. getter

前端 未结 8 921
臣服心动
臣服心动 2020-11-30 04:48

I need a small Container-Class for storing some Strings which should be immutable. As String itself is an immutable type, I thought of something like that:

p         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 05:14

    The problem is the uniform access principle. You may later need to modify foo so that it's obtained through a method instead of being fixed, and if you exposed the field instead of a getter, you'll need to break your API.

提交回复
热议问题