Local classes can most definitely reference instance variables. The reason they cannot reference non final local variables is because the local class instance can remain in memory after the method returns. When the method returns the local variables go out of scope, so a copy of them is needed. If the variables weren’t final then the copy of the variable in the method could change, while the copy in the local class didn’t, so they’d be out of synch. Anonymous inner classes require final variables because of the way they are implemented in Java. An anonymous inner class (AIC) uses local variables by creating a private instance field which holds a copy of the value of the local variable. The inner class isn’t actually using the local variable, but a copy. It should be fairly obvious at this point that a “Bad Thing”? can happen if either the original value or the copied value changes; there will be some unexpected data synchronization problems. In order to prevent this kind of problem, Java requires you to mark local variables that will be used by the AIC as?final?(i.e., unchangeable). This guarantees that the inner class’ copies of local variables will always match the actual values.
详细解决方案
why inner class can access only final variable
热度:9169 发布时间:2013-02-26 00:00:00.0
相关解决方案
- Class not found: com.mchange.v2.c3p0.ComboPooledDataSource,该怎么处理
- org.apache.jasper.JasperException: Unable to compile class for JSP:该怎么解决
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password,该如何处理
- java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 找不到资料 '(未知的)'
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password解决思路
- org.apache.jasper.JasperException: Unable to compile class for JSP:解决思路
- !使用JDNI时,报 Cannot create JDBC driver of class '' for connect URL 'null'
- Unable to load class for JSP,该如何处理
- 新手Dialect class not found
- Caused by: org.hibernate.MappingException: Association references un地图ped class: com.elone.pm.product.dao.TAttribute
- jsp access 图片调用的有关问题
- Error creating bean with name 'sessionFactory' defined in class path resource解决方法
- Unable to compile class for JSP找不出异常
- 小弟我用JSP+ACCESS+tomcat 5.0 JDK1.4做的一个网站
- Could not load JDBC driver class 咋回事
- No setter found for property 'sessionFactoroy' in class 'com.iBBS.dao.impl.UserI解决思路
- getHibernateTemplate().get(entity.Class,serializabel id) 如何报java.lang.NullPointerException的错误
- jsp中出现the local variable zhang is never read求大神见教
- 小弟我在tomcat里的Server.xml部署web项目报Error instantiating servlet class
- Jsp + Oracle 怎么取回id,报错getInt not implemented for class oracle.jdbc.driver.T4CRo
- ssh项目hibernate 异常:org.hibernate.MappingException: Association references unmapped class: Message
- java.lang.NoClassDefFoundError: Could not initialize class cn.elvis.utils.JdbcUt,该如何处理
- Cannot load JDBC driver class 'com.microsoft.jdbc.sqlserver.SQLServerDriver'解决方法
- class path resource [applicationContext.xml] does not exist解决方法
- 出现异常:cvs [server aborted]:"passwd" requires write access to the repository
- ids for this class must be manually assigned before calling save(): po.Project解决方法
- java面试例题惑 之变量初始顺序,变量覆盖,字符串池,final,finally区别