Immutable Type: public final fields vs. getter

前端 未结 8 908
臣服心动
臣服心动 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: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 ; }
           }
    }
    

提交回复
热议问题