final的功能: 根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类、非抽象类成员方法和变量。你可能出于两种理解而需要阻止改变:设计或效率。- package com.exam;
- public class test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- final student stu1=new student("zhang",1);
- System.out.println(stu1.getName()+" "+stu1.getID());
-
- stu1.setName("wang");
- System.out.println(stu1.getName()+" "+stu1.getID());//这样打印出来的stu1就发生改变了,感觉就辜负了final字的功能
-
-
-
-
-
- }
- }
- class student
- { String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getID() {
- return ID;
- }
- public void setID(int iD) {
- ID = iD;
- }
- int ID;
- student(String name,int ID){
- this.name=name;
- this.ID=ID;
- }
- }
复制代码 |