Java final 关键字理解 博客分类: Java
public class TestFinal {
private static final String name = "testName";
private static final Person person = new Person("Test person name",18);
public static void main(String[] args) {
System.out.println("TestFinal.name:"+TestFinal.name);
//The final field TestFinal.name cannot be assigned
//TestFinal.name = "Test Name Change";
System.out.println("TestFinal.person.getName():"+TestFinal.person.getName());
System.out.println("TestFinal.person.getAge():"+TestFinal.person.getAge());
TestFinal.person.setName("Test person name changed");
TestFinal.person.setAge(20);
System.out.println("After changed value TestFinal.person.getName():"+TestFinal.person.getName());
System.out.println("After changed value TestFinal.person.getAge():"+TestFinal.person.getAge());
}
}
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/*运行后为:
TestFinal.name:testName
TestFinal.person.getName():Test person name
TestFinal.person.getAge():18
After changed value TestFinal.person.getName():Test person name changed
After changed value TestFinal.person.getAge():20
*/
推荐阅读
-
Java final 关键字理解 博客分类: Java
-
视图和事物 博客分类: java
-
java 常用工具包(自己拓展) 博客分类: java JavamavenSVNEclipse项目管理
-
freemark java 一个简单例子 博客分类: freemark JavaStrutsfreemarkerServletApache
-
Hibernate入门示例(架构图) 博客分类: java HibernateJava数据结构企业应用Servlet
-
java swing 下拉框增加回车事件 博客分类: java swingJava 下拉框listbox下拉列表回车事件VK_ENTER
-
JAVA CMS系统详细设计+源码(开发中...) 博客分类: freemark CMSJava配置管理项目管理QQ
-
ibatis3 简单示例 博客分类: java JavaiBATISSQLDAOJSP
-
java cms MyEclipse6.0 工程代码 博客分类: java CMSJava搜索引擎Oraclefreemarker
-
Dwr入门例子(附源代码下载-JBuilder2006工程)完成 博客分类: java DWRiBATIS配置管理数据结构Servlet