1、fiannal修饰的类无法继承;
2、final修饰的方法无法重写;
3、final修饰的变量只能赋值一次;
4、final修饰的引用一旦指向某个对象,则不能再重新指向其它对象,但该引用指向的对象内部的数据是可以修改的。
@Test void test01() { Person p1=new Person(); p1.setAge(15); p1.setName("wanger"); final Person p2=p1; System.out.println("p1:"+JSON.toJSONString(p1)); System.out.println("p2:"+JSON.toJSONString(p2)); p1.setAge(55); System.out.println("p1:"+JSON.toJSONString(p1)); System.out.println("p2:"+JSON.toJSONString(p2)); } class Person{ private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
p1:{"age":15,"name":"wanger"} p2:{"age":15,"name":"wanger"} p1:{"age":55,"name":"wanger"} p2:{"age":55,"name":"wanger"}