final可以修饰类,方法,成员变量。
final修饰的类为终态类,不能被继承。
final修饰的方法为终态的方法,不能被重写。
final修饰的成员变量为不可更改值。一旦更改就报错。
final 修饰的成员变量赋值情况有两种
1.在成员变量创建时就赋值
1.在成员变量创建时就赋值
final int a=0;
2.成员变量不赋值,在所有构造函数中赋值
class Name
{
final int a;
Name()
{
a=0;
}
Name(int b)
{
b=0;
a=0;
}
}
当一个类被修饰为 final引用,该引用不能创建新的对象,该引用的成员变量可以更改。
class Son
{
final Father father=new Father();
}
class Father
{
int age=10;
}
public class Final
{
public static void main (String[] args)
{
Son son=new Son();
son.father.age=100;
System.out.println(son.father.age);
}
}