depth understanding of java final keyword

Basic understanding:

  1. Modified class

    When a final modification to a class, indicates that the class can not be inherited. In security, in the JDK, it is designed to have a String final class, System, etc. These classes can not be inherited. Note: The modified final member of the class may be modified, it may be not.

  2. modification methods:

     Method can not be overridden by subclasses. Commonly used in the class do not want overridden by subclasses and modified design.

  3. A method of modifying parameters:

     The modified parameter variables can not be assigned again in vivo. This seems to be standing on the caller's point of consideration Ha, it seems to have a big brother took the knife to me and told me to see people, big brother said, you must use the knife to chop it, not to change the Tulong, do not want to change the machine gun, or you're dead. The total did not feel that this final modification of the parameter value where, do not know the place did not understand.

  4. modified member variable.

      Modified member variables can only be assigned once. It will be assigned before class initialization.

Deep understanding, and use

  The main use of some features modified member variables (personally think this is more used) use in java.

  1. The use of a modified final member variables must be initialized in the class assignment, inheritance using the design class.

    Scenario: There is a field field is required in the design of class A of the time. But this class may have many subclasses, and the value of each subclass is not the same. Then you can design the fileld modified by the final. Assignment in the subclass initialization time. As the code, I assume that person's name is the name must design the Person class, but the name of each person is different, so with final modification field name, let the name assigned in the constructor. So subclass inherits Person must call the Person constructor to name assignment. We do have a bug that would lose the use of no-argument constructor.

public  class the Person {
     Final String name;    // no assignment, it must be assigned in the constructor. 
    public the Person {(String name)
         the this .name = name;
    }
    public void worlk() {

    }
}
public class Student extends Person {

    public Student (String name) {
         Super (name); // you must call the parent class constructor 
    }
    
    
    @Override
    public String toString() {
        return "Student [name=" + name + "]";
    }
    public static void main(String[] args) {
        Student s = new Student("A");
        Student s1 = new Student("B");
        System.err.println (S); // Output: Student [name = A] 
        System.err.println (S1); // Output: Student [name = B] 
    }
}

  2.final keyword special significance concurrent programming code from the "java Multithreaded Programming Guide"

    

public class FinalFieldExample {
  final int x;
  int y;
  static FinalFieldExample instance;

  Public Final Field Example () {
    x = 1;
    and = 2 ;
  }

  public static void writer() {
    instance = new FinalFieldExample();
  }

  public  static  void Reader () {
     Final FinalFieldExample = theInstance instance;
     IF (theInstance =! null ) {
       int diff = theInstance.y - theInstance.x;
       // the diff may be 1 (= 2-1), or may be -1 (= 0-1). 
      print (diff);
    }
  }

  private static void print(int x) {
    // ...
  }
}

  More concurrent programming there will be a security risk, of course, there will not be such a code design in the event. The book is only used for Liezi reference. In the compiler to initialize an object when, in fact, not an atomic operation (for members of the object). In the above code to initialize FinalFieldExample time, the equivalent of about pseudo-code:

 = the allocate objRef (FinalFieldExample. class ); // sub-operation 1: object storage space required for allocation 
  objRef.x = 1; // sub operation 2: object initialization 
  objRef.y = 2: // sub operation 3: object initialization 
  = objRef instance; // sub operation 4: assign an object to a reference variable

 

 But sometimes the compiler will optimize the code, recompile. The variable y is assigned after object initialization is complete, that is, there may be a third step, before step will be after the fourth. This may be performed to complete the fourth step of this moment, another thread calls this object, then the value of the default initial value of y is 0 instead of the expected value of 2. But the value of the variable X can be guaranteed, because the final modification, must be assigned before FinalFieldExample initialization, can not be optimized to ensure that Step 2 to Step 4 After sorting, and ensure visible to other threads.

  All the above codes, y after final modification also, the calling thread can be guaranteed to get the desired value y = 2; x = 1;

 

3. Design Security objects: immutable objects.

 

  pending upgrade...

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

Guess you like

Origin www.cnblogs.com/jonrain0625/p/11324676.html