![](/img/trans.png)
[英]Is there a way for anonymous inner classes in Java to “lose” their scope?
[英]Java Scope and Lifetime (inner classes)
我需要解释为什么以下代码将无法编译(就范围和生存期而言):
class ClassInMethod
{
public static void main(String[] args)
{
int local = 1;
class Inner
{
public void method()
{
System.out.println(local);
}
}
}
}
我认为这是因为:任何在内部类中使用但未声明的局部变量都必须声明为“ final”。 因此,在此示例中,“ local”必须声明为final,因为它的范围和生存期在main方法内(因此需要更改为:final int local = 1;)。
还有其他建议吗?
请参阅http://techtracer.com/2008/04/14/mystery-of-accessibility-in-local-inner-classes/了解有关编译器如何使用final
技巧来解决内部类问题的说明(例如,作用域和一生的问题)。
只是尝试编译它。 编译器输出:
ClassInMethod.java:11: local variable local is accessed from within inner class; needs to be declared final System.out.println(local); ^ 1 error
您必须使局部变量成为final
变量的原因是Java将其值复制到内部类的实例中。 幕后发生的事情是,编译器生成(大致)与此对应的字节码:
class ClassInMethod {
public static void main(String[] args) {
int local = 1;
// this happens when you do: new Inner()
ClassInMethod_main_Inner inner = new ClassInMethod_main_Inner();
inner.local = local;
}
}
// the inner class
class ClassInMethod_main_Inner {
int local;
public void method() {
System.out.println(local);
}
}
如果local
没有final
,你可以改变它的当值之间Inner
被实例化,并在method()
被调用,调用method()
会使用的旧值local
。 这可能是不正确的行为。 要求final
的原因是为了使内部类的行为更直观。
(有些语言没有此限制,但是它需要编译器和运行时的显式支持。到目前为止,Java的开发人员尚未决定致力于实现它。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.