final definitions of key words and the use of

1. The keyword modified local variables, then there may be a change after the assignment

2. The keyword modified class can not be inherited

3. The keyword modification methods can not be overridden

4. The members of keywords modified method must be initialized

The basic data types are immutable the contents, types and the reference data which is immutable reference to the designated reference target content is variable.

Meaning the final keyword?

 

final in Java is a reserved keyword, you can declare member variables, methods, classes, and local variables. Once you quote a statement made final, you will not be official Benten this reference, the compiler will check the code, if you will try to re-initialize a variable, then the compiler will report compilation errors.

final modification of variables: final int i = 18;

This keyword modified class: public final class {} class name, the method name declared in the main front and a variable type

What is the final method?

 

The method can also be declared final. Plus the final in front of the keyword method, this method may not be representative of the quilt class override the method. If you think a function of the method has been complete enough, subclasses need to change it, you can declare this method is final. final method is faster than the non-final method, because at compile time has been static binding, and do not need dynamic binding at runtime.

 

class Person{
  public final String getName(){        return "personal loan"
    }
}class loan extends Person{
      @Override
        public final Stirng getName(){                return "cheap personal loan";//compilation error: overridden method is final
        }
}

 

What is the final category?

 

Use final to modify the class called final classes. final class usually is fully functional, they can not be inherited. There are many Java class is final, such as String, Interger and other packaging.

 

final class PersonalLoan{

   }  
class CheapPersonalLoan extends PersonalLoan{ }

 

Immutable class

 

Create an immutable class to use the final keyword. Immutable category refers to its objects once created can not be changed. String is immutable class representatives. Immutable class has many advantages, such as their object is read-only and can be safely shared in a multithreaded environment without additional synchronization overhead, and so on.

 

With the final modification of parameters within the class is very useful in the anonymous inner classes in order to maintain the consistency of the parameters of the method if the shape parameters need to be located inside the inner classes, this parameter must be as final.

 

Until there is no explanation why the final? Both attribute parameters and external methods within the class is the same thing from the outside, but in fact are not, so they both can be arbitrarily changed, which means that I change the properties of the inner class It does not affect the external parameter , and then it from the programmer's point of view this is not feasible, after standing in a procedural point of view these two is simply the same, if the inner class has changed, and external method did not change the parameter which is difficult to understand and unacceptable, in order to maintain the consistency of the parameters, to a predetermined final use to avoid parameter does not change.

Simple to understand, a reference copy, change the reference value in order to avoid the occurrence of such a method is modified outside the class or the like, and an inconsistent value obtained inner class, then use this reference is not to make final changes.
    

 If it defines an anonymous inner classes, and it is desirable to use a definition of the external parameter, the compiler may require this parameter reference is final.

 

We do not need so much, just know that this final is immutable meaning on it

Guess you like

Origin www.cnblogs.com/JiXianSen/p/12142259.html