Defensive copy from Effective Java

后端 未结 5 1005
庸人自扰
庸人自扰 2020-12-15 19:06

I am reading \"Effective Java\" by Joshua Bloch, item 39 make defensive copy, and I have some questions. I always use the following construct:

MyObject.getSo         


        
5条回答
  •  情深已故
    2020-12-15 19:32

    call getSomeRef() two times and compare there reference if they are different then function is returning the copy else it is returning the same instance.

    if(MyObject.getSomeRef() == MyObject.getSomeRef()){
         // same instance
    }else{
         // copied instance
    }
    

提交回复
热议问题