Fork me on GitHub

final--finally--finalize


final修饰符(关键字)

①如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承。
因此一个类不能既被声明为 abstract的,又被声明为final的。

②将变量或方法声明为final,可以保证它们在使用中不被改变。

final方法

 1 public class Test1 { 
 2 public static void main(String[] args) { 
 3     // TODO 自动生成方法存根 
 4 } 
 5 public void f1() { 
 6     System.out.println("f1"); 
 7 } 
 8 //无法被子类覆盖的方法 
 9 public final void f2() { 
10     System.out.println("f2"); 
11 } 
12 public void f3() { 
13     System.out.println("f3"); 
14 } 
15 private void f4() { 
16     System.out.println("f4"); 
17 } 
18 } 
19 public class Test2 extends Test1 { 
20     
21 public void f1(){     
22     System.out.println("Test1父类方法f1被覆盖!"); 
23 } 
24 public static void main(String[] args) { 
25     Test2 t=new Test2(); 
26     t.f1();    
27     t.f2(); //调用从父类继承过来的final方法 
28     t.f3(); //调用从父类继承过来的方法 
29     //t.f4(); //调用失败,无法从父类继承获得 
30 } 
31 }
View Code

 

final参数

public class Test4 { 
        public static void main(String[] args) { 
                new Test4().f1(2); 
        } 

        public void f1(final int i) { 
                //i++;    //i是final类型的,值不允许改变的. 
                System.out.print(i); 
        } 
}
View Code

final变量(常量)

 1 package org.leizhimin; 
 2 
 3 public class Test3 { 
 4         private final String S = "final实例变量S"; 
 5         private final int A = 100; 
 6         public final int B = 90; 
 7 
 8         public static final int C = 80; 
 9         private static final int D = 70; 
10 
11         public final int E; //final空白,必须在初始化对象的时候赋初值 
12 
13         public Test3(int x) { 
14                 E = x; 
15         } 
16 
17         /** 
18          * @param args 
19          */ 
20         public static void main(String[] args) { 
21                 Test3 t = new Test3(2); 
22                 //t.A=101;    //出错,final变量的值一旦给定就无法改变 
23                 //t.B=91; //出错,final变量的值一旦给定就无法改变 
24                 //t.C=81; //出错,final变量的值一旦给定就无法改变 
25                 //t.D=71; //出错,final变量的值一旦给定就无法改变 
26 
27                 System.out.println(t.A); 
28                 System.out.println(t.B); 
29                 System.out.println(t.C); //不推荐用对象方式访问静态字段 
30                 System.out.println(t.D); //不推荐用对象方式访问静态字段 
31                 System.out.println(Test3.C); 
32                 System.out.println(Test3.D); 
33                 //System.out.println(Test3.E); //出错,因为E为final空白,依据不同对象值有所不同. 
34                 System.out.println(t.E); 
35 
36                 Test3 t1 = new Test3(3); 
37                 System.out.println(t1.E); //final空白变量E依据对象的不同而不同 
38         } 
39 
40         private void test() { 
41                 System.out.println(new Test3(1).A); 
42                 System.out.println(Test3.C); 
43                 System.out.println(Test3.D); 
44         } 
45 
46         public void test2() { 
47                 final int a;     //final空白,在需要的时候才赋值 
48                 final int b = 4;    //局部常量--final用于局部变量的情形 
49                 final int c;    //final空白,一直没有给赋值.    
50                 a = 3; 
51                 //a=4;    出错,已经给赋过值了. 
52                 //b=2; 出错,已经给赋过值了. 
53         } 
54 }
View Code

 

 

被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。

 

④被声明为final的方法也同样只能使用,不能重载。
finally—再异常处理时提供 finally 块来执行任何清除操作。如果抛出一个异常,那么相匹配的 catch 子句就会执行,
然后控制就会进入 finally 块(如果有的话)。

finalize—方法名。Java 技术允许使用 finalize() 方法在垃圾收集器将
对象从内存中清除出去之前做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对这个对象调用的。
它是在 Object 类中定义的,因此所有的类都继承了它。子类覆盖 finalize() 方法以整理系统资源或者执行其他清理工作。
finalize() 方法是在垃圾收集器删除对象之前对这个对象调用的。

posted @ 2015-11-05 10:38  ZZZZW  阅读(181)  评论(0编辑  收藏  举报
AmazingCounters.com