Immutable Type: public final fields vs. getter

前端 未结 8 924
臣服心动
臣服心动 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:01

    I would do what you believe is simplest and clearest. If you have a data value class which is only used by a restricted number of classes. esp a package local class. then I would avoid getter/setters and use package local or public fields.

    If you have a class which you expect other modules/developers to use, following a getter/setter model may be a safer approach in the long run.

提交回复
热议问题