Immutable Type: public final fields vs. getter

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

    It is not very clear if someone is going to use your code through an API. You are also missing an opportunity to validate the input, if you are going to require some later.

    0 讨论(0)
  • 2020-11-30 05:16

    This answer is obviated:

    Why not

    interface Immu { String getA() ; String getB ( ) }
    
    Immu immu ( final String a , final String b )
    {
           /* validation of a and b */
           return new Immu ( )
           {
                  public String getA ( ) { return a ; }
    
                  public String getB ( ) { return b ; }
           }
    }
    
    0 讨论(0)
提交回复
热议问题