final关键字的使用

package com.aff.fin;

/*
 final:最终的,可以用来修饰类,属性,方法
     1.final修饰类:这个类不能被继承,如:String类,StringBuffer类,System类
     2.final修饰方法:不能被重写。如:Object类的getClass()
     3.final修饰属性:此属性就是一个常量。一旦初始化后,不可以再被赋值。习惯上,常量用大写字符表示
       此常量不能使用默认初始化,可以显式的赋值,代码块,构造器 ,在创建对象之前把final给赋值。
     4.变量用static final修饰:全局常量
     
 finally         异常使用的
 finalize()方法    垃圾回收使用的
 */
public class TestFinal {
    public static void main(String[] args) {
        D d =  new D();
        System.out.println(d.getClass());
        System.out.println(Math.PI);//就表示一个常量 谁都不能更改
    }
}

/*
  class SubString extends String{ 
  //The type SubString cannot subclass the
  //final class String 为final修饰无法被继承

  }
 */
class D {
    final int I = 12;
    final double  PI;
    final String NAME;
    
    public D(){
        NAME = "BBB";
    }
    
    public D(String name){
        this();
        //NAME = name;
    }
    
    public void v1() {
        System.out.println(I);
        // I = 19;//不行的
    }
    {
    PI=3.14;    
    }
}
输出结果:

class com.aff.fin.D
3.141592653589793

 
All that work will definitely pay off
原文地址:https://www.cnblogs.com/afangfang/p/12532416.html