Why final instance class variable in Java?

后端 未结 11 697
南方客
南方客 2020-12-09 05:13

If instance variable is set final its value can not be changed like

public class Final {

    private final int b;

    Final(int b) {
        this.b = b; 
         


        
相关标签:
11条回答
  • 2020-12-09 05:23

    This are the usages of the keyword final that I know:

    In the class declaration

    This means that the class: cannot be sub classed

    public final class SomeClass {
    //...
    }
    

    In a global variable

    This means that once a value is assigned to it, it cannot change.

    public class SomeClass  {
         private final int value = 5;
    }
    

    There will be a compile error if you don't assign the value, but what you can do is use composition to give a value.

    public class SomeClass  {
         private final int value;
         public SomeClass(int value) {
           this.value=value
          }
    }
    

    In an object parameter

    This means that the passed object cannot be changed

    public class SomeClass {
       public void someMethod(final String value) {
          //...
       }
    }
    

    In local variables

    This means that the value cannot change once assigned

    public class SomeClass {
       public void someMethod(final String value) {
          final double pi = 3.14;
       }
    }
    

    In methods

    This means that the method cannot be overriden

     public class SomeClass {
               public final void someMethod() {
                  //...
               }
            }
    

    In collections & maps

    This means that the collection cannot be reinitialized, but it doesn't mean that the elements are inmutable, each of the elements will not be afected by the keyword final

     public class SomeClass {
           final HashMap<K, V> someMap = new HashMap<K, V>();
      }
    
    0 讨论(0)
  • 2020-12-09 05:24

    When a reference is final, it cannot be linked to any other object.

    The values of the object can be changed, so you can add values to the map, but cannot change the object of the map.

    0 讨论(0)
  • 2020-12-09 05:27

    final has nothing to do with the contents of the object the variable is referring to. You will only not be able to change the value of the variable and make it refer to another object.

    0 讨论(0)
  • 2020-12-09 05:27

    It means that the hasmap cannot be changed. The elements inside the hashmap are not tied to the final delimiter.

    private static final HashMap<K, V> map = new HashMap<K, V>();
    
    public void foo(K k, V v) {
        map.push(k, v);  //This is allowed
        map = new HashMap<K, V>  //This is not allowed
    }
    
    0 讨论(0)
  • 2020-12-09 05:29

    In "C/C++" terms:

    Thing * a;
    Thing * const b;
    Thing const * c;
    Thing const * const d;
    

    The "final" in Java is closest to "b". "b" is a constant pointer to a Thing. "b" cannot be changed to point to a different Thing, but the Thing itself may be changed.

    Java doesn't have a representation for "c" and "d". "c" is a pointer to a constant Thing. "c" may point to other Things, but the Things it points to cannot be changed (at least, not through "c" itself)

    "d" combines "b" and "c": "d" is a constant pointer to a constant Thing.

    Oh, and "a" of course is just nothing special.

    Hm...In Java, not everything is an object so the rules are a little different.

    final int f = 9;
    

    Which, in C is much like

    int const f = 9;
    

    Which means you cannot change "f" or its integer value.

    NOTE:

    int const f;
    const int g;
    

    both mean the same thing, but "f" IMHO has clearer meaning. "g" is unfortunately very common.

    0 讨论(0)
  • 2020-12-09 05:34

    I think you are getting confused between "final" and "immutable" objects..

    public class Final {
    
         private final int b;
    
         Final(int b) {
                this.b = b; 
         }
    
         int getFinal() {
              return  b = 8;  // COMPILE TIME ERROR 
         }
    }
    

    Final means you cannot change the reference to the object. In case of primitives, it means you cannot change the value. So, when you try to set b to 8, you get a compile time error.

    cc.setConditionMap(cacheMap);
    
    public void setConditionMap(Map conditionMap) {
            this.conditionMap = conditionMap;
    }
    

    In Java - "References to objects are passed by value" (As Bruce Eckel puts it in his book "Thinking in Java"). So, you are passing a copy of the reference. So, you have 2 references to the same cacheMap now.

    So, you can change the cacheMap using any of the references. But you can reassign only the "copied" reference to another object, as it is not final (not the original one, the original one is final and CANNOT point to another object).

    0 讨论(0)
提交回复
热议问题