java:从内部类中访问本地变量msg,需要被声明为最终类型
2016-11-18 17:20
1026 查看
简单来说就是因为之前的java并不支持闭包,目前java8添加了对函数式编程的支持,所以已经不需要这样了。
参考资料:
java - Why are only final variables accessible in anonymous class?
java - Cannot refer to a non-final variable inside an inner class defined in a different method
Java doesn't support true
closures, even though using an anonymous class like you are using here (new TimerTask() { ... }) looks like a kind of closure.
edit - See the comments below - the following is not a correct explanation, as KeeperOfTheSoul points out.
This is why it doesn't work:
The variables lastPrice and price are local variables in the main() method. The object that you create with the anonymous class might last until after the main() method returns.
When the main() method returns, local variables (such as lastPrice and price) will be cleaned up from the stack, so they won't exist anymore after main() returns.
But the anonymous class object references these variables. Things would go horribly wrong if the anonymous class object tries to access the variables after they have been cleaned up.
By making lastPrice and price
and you won't have the problem with accessing non-existent variables anymore.
Other programming languages that do support closures do it by treating those variables specially - by making sure they don't get destroyed when the method ends, so that the closure can still access the variables.
@Ankur: You could do this:
来自:http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen
参考资料:
java - Why are only final variables accessible in anonymous class?
java - Cannot refer to a non-final variable inside an inner class defined in a different method
Java doesn't support true
closures, even though using an anonymous class like you are using here (new TimerTask() { ... }) looks like a kind of closure.
edit - See the comments below - the following is not a correct explanation, as KeeperOfTheSoul points out.
This is why it doesn't work:
The variables lastPrice and price are local variables in the main() method. The object that you create with the anonymous class might last until after the main() method returns.
When the main() method returns, local variables (such as lastPrice and price) will be cleaned up from the stack, so they won't exist anymore after main() returns.
But the anonymous class object references these variables. Things would go horribly wrong if the anonymous class object tries to access the variables after they have been cleaned up.
By making lastPrice and price
final, they are not really variables anymore, but constants. The compiler can then just replace the use of lastPrice and price in the anonymous class with the values of the constants (at compile time, ofcourse),
and you won't have the problem with accessing non-existent variables anymore.
Other programming languages that do support closures do it by treating those variables specially - by making sure they don't get destroyed when the method ends, so that the closure can still access the variables.
@Ankur: You could do this:
public static void main(String args[]) { int period = 2000; int delay = 2000; Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { // Variables as member variables instead of local variables in main() private double lastPrice = 0; private Price priceObject = new Price(); private double price = 0; public void run() { price = priceObject.getNextPrice(lastPrice); System.out.println(); lastPrice = price; } }, delay, period); }
来自:http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen
相关文章推荐
- 错误: 从内部类中访问本 地变量vvv; 需要被声明为最终类型
- 【面试对宝典40题的解释】为什么从内部类中访问局部变量需要被声明为final最终类型
- 为什么从内部类中访问局部变量需要被声明为最终类型
- 为什么从内部类访问局部变量,需要被声明为最终类型?
- JAVA匿名内部类不能访问外部类方法中的局部变量,除非变量被声明为final类型
- 【Java】内部类访问的外部变量定义成final (数据保持一致,外部修改后无法通知内部,编译的时候会通过构造方法传进来) Java内部类一般访问不了外部变量
- 内部类访问的外部变量需要使用final修饰解释原因
- 在静态方法中使用变量 (需要将变量在.m文件中声明未静态static 类型)
- 能用foreach遍历访问的对象需要实现____接口或声明____方法的类型
- JAVA 对象定义、访问对象、基本类型和引用类型变量区别
- JAVA-变量声明、关键字和类型
- 能用foreach遍历访问的对象需要实现 ________________接口或声明________________方法的类型。
- 为什么在方法中定义的内部类只能访问方法中的final类型的局部变量?
- 注意java8中已经没有匿名内部类和局部内部类只能访问final变量的限制了!
- 为什么匿名内部类只能访问其所在方法中的final类型的局部变量?
- Java基础-内部类-为什么局部和匿名内部类只能访问局部final变量
- 边打dota边学java 第二篇(java中变量类型及变量声明格式)
- 方法中的内部类只能访问final类型的变量
- 为什么内部类访问的外部变量需要使用final修饰
- Java方法内部类为什么需要方法形参或变量是final的?