zoukankan      html  css  js  c++  java
  • Use final liberally

     
    Use the final keyword liberally to communicate your intent.

    The final keyword has more than one meaning:

    • final class cannot be extended
    • final method cannot be overridden
    • final fields, parameters, and local variables cannot change their value once set
    In the last case, "value" for primitives is understood in the usual sense, while "value" for objects means the object's identity, not itsstate. Once the identity of a final object reference is set, it can still change its state, but not its identity (that is, you can't re-point the object reference to some other object).

    Declaring primitive fields as final automatically ensures thread-safety for that field.

    Some habitually declare parameters as final, since this is almost always the desired behaviour. Others find this verbose, and of little real benefit.

    Consistently using final with local variables (when appropriate) can be useful as well. It brings attention to the non-final local variables, which usually have more logic associated with them (for example, result variables, accumulators, loop variables). Many find this verbose. A reasonable approach is to occasionally use final for local variables, but only if there is some unusual condition, whereby making final explicit can call attention to at least one non-final local variable in the method; this serves to quickly distinguish the non-final local variables from the others.

    Using final:

    • clearly communicates your intent
    • allows the compiler and virtual machine to perform minor optimizations
    • clearly flags items which are simpler in behaviour - final says, "If you are looking for complexity, you won't find it here."
    Example 
    import java.util.*;
    import java.lang.reflect.Field;
    
    /** This class cannot be extended, since it's final. */
    public final class Boat {
    
      public Boat(final String aName, final int aLength, final Date aDateManufactured){
        fName = aName;
        fLength = aLength;
        //make a defensive copy of the date
        fDateManufactured = new Date(aDateManufactured.getTime());
    
        //does not compile, since the items are final:
        //aDateManufactured = null;
        //aLength = 0;
      }
    
      /** Cannot be overridden, since the class itself is final. */
      public void setDate(final Date aNewDate){
        //even though the field is final, its state can change:
        fDateManufactured.setTime(aNewDate.getTime());
    
        //does not compile, since field is final:
        //fDateManufactured = aNewDate;
      }
    
      /** Return the highest race score. */
      public Integer bestRaceScore(){
        //the result reference can't be final, since it can be 
        //re-pointed to different objects
        Integer result = Integer.valueOf(0); 
        //final Integer result = Integer.valueOf(0); //doesn't compile
        
        //this example is artificial, since fRaceScores could be 
        //referenced directly here...
        final List<Integer> scores = fRaceScores;
        for(Integer score : scores){
          if (score > result){
            result = score; //re-point to the max value
          }
        }
        return result;
      }
      
      //..elided
    
      // PRIVATE
      private final String fName;
      private final int fLength;
      private List<Integer> fRaceScores = new ArrayList<>();
      private final Date fDateManufactured;
    }  
    

      

  • 相关阅读:
    必知必会 | Android 性能优化的方面方面都在这儿
    上周热点回顾(1.19-1.25)团队
    上周热点回顾(1.12-1.18)团队
    如何在博客园的markdown编辑器中输入数学公式团队
    上周热点回顾(1.5-1.11)团队
    上周热点回顾(12.29-1.4)团队
    上周热点回顾(12.22-12.28)团队
    云计算之路-阿里云上-寒流来袭:2014年12月23日21:45-23:15网站故障团队
    上周热点回顾(12.15-12.21)团队
    【活动】加班一整年了,程序员们,你们还好吗?团队
  • 原文地址:https://www.cnblogs.com/hephec/p/4579573.html
Copyright © 2011-2022 走看看