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
相关解决方案
- !使用JDNI时,报 Cannot create JDBC driver of class '' for connect URL 'null'
- 新手Dialect class not found
- jsp access 图片调用的有关问题
- 小弟我用JSP+ACCESS+tomcat 5.0 JDK1.4做的一个网站
- Could not load JDBC driver class 咋回事
- jsp中出现the local variable zhang is never read求大神见教
- Cannot load JDBC driver class 'com.microsoft.jdbc.sqlserver.SQLServerDriver'解决方法
- java面试例题惑 之变量初始顺序,变量覆盖,字符串池,final,finally区别
- 初学急求(待高手指导)java.lang.InstantiationException: Class not a MIDlet?已经写了MIDlet类,如何修改
- 用eclipse打包jar后运行,弹出异常could not find main class
- eclipse class not found 愁死了解决方法
- class.formName获取的种能做List的泛型类么
- There is no WRITEABLE property named 'outboxsize' in class 咋回事
- java 无构造 final 类,该怎么处理
- public final static day解决方案
- 报错“需要.class”!新手、求高手给解!该如何解决
- 何为原子访问(atomic access)解决方案
- Sub Class 与 Child Class的差异
- 作甚原子访问(atomic access)
- Netbeans 7.0 生成的jar运作总是找不到main class
- 请问,这样定义一个变量,是啥意思?Class<> waiClass
- 关于xxxxx.class.getClassLoader().getResource(fileName),
- static final int i;若不赋值就失误,求内存解释
- 继续:vs2005 vc access 取不到表中的数据,该如何解决
- vs 2005 对一个新键Dialog的没法使用Add Class,不知道说明原因
- 持续:vs2005 vc access 取不到表中的数据
- vs2005,vc access MoveNext()
- dax error : access violation at address解决办法
- Delphi程序写的EXCEL导入程序时报错:Access violation at address0057416f in module"serv"exe,该怎么解决
- dax error : access violation at address,该如何解决