The final 24static and final keywords

final is the ultimate meaning, it is a keyword is a modifier

It can be modified data, methods, classes

1.final modified data

final modification is the basic data type, the final value is not changed (final modified data can be the first statement, go to the assignment, the assignment only once)

    final  int i = 2 ;
         // i = 4; // statement error, data modified final variables can not be changed --- the final value 
        final  int J; // final statement before data can be modified, go to the assignment, the assignment only a 
        j = 6;

final modified reference data types

final modified reference data types, the final value is a reference to the address, the address value can not be changed

final  int [] ARR = {l, 2,3 };
         // element value assignment, this time value in the array may be changed, then the final value is not modified array elements 
        ARR [2] =. 6 ;   
        ARR [ 0 ] =. 5 ; 
         // ARR = new new int [. 5]; // this statement error, the address value changes.
        // arr.length =. 5; // this statement error, the array length is also the final value.

final modification of the value of a member variable

Before the assignment will create objects (block constructor and optionally one configuration and only once assigned)

class A {
     Final  int I; 
    { 
        I =. 4; // configuration code block assignment 
    }
     public A () {
         // I =. 5; // configuration code block and a constructor, whichever comes assignment 
    } 
}

static and final common modification (static final): Before class assignment will be loaded once and as long as the assignment

class A{
    static final int i;
    static 
    {
        i=4;
    }
    {
        
    }
    public A() {
    
    }
}

2.final modification methods

final modification methods then this method can be overloaded and rewrite it

You can override

It can be inherited but can not override

class A {
     // overloaded 
    public  Final  void m () {}
     public  Final  void m ( int I) {} 
    
} 
class B the extends A {
     // annotation judges whether the reload 
    @Override
     // public void Final m () { } // overloaded being given 
}

 

3.final modified class

final modified class can not be inherited, but can inherit another class

Final  class A {
     // attribute 
    String name;
     // Method 
    public  void m () {}
     // constructor with no arguments 
    public A () { 
        
    } 
    
} 
/ * class B {// the extends A final class can not inherit A 
    
} * /

 

Guess you like

Origin www.cnblogs.com/xuwangqi/p/11098655.html