是p所指向的对象不能改变,p内的变量x可变。如代码:- public class Demo
- {
- public static void main(String[] args)
- {
- //Person p = new Person();
- //p.setX(10);
- final Person p = new Person();
- //p = new Person();//无法为最终变量p分配值
- p.setX(12);
- System.out.println(p.getX());
- }
- }
- class Person
- {
- int x=3;
- public void setX(int x)
- {
- this.x=x;
- }
- public int getX()
- {
- return x;
- }
- }
复制代码 |