为什么在方法中定义的内部类只能访问方法中的final类型的局部变量?


class InOut
{
	String str=new String ("Between");
	public void amethod(final int iArgs)
	{
		int it315=10;
		class Bicycle
		{
			public void sayHello()
			{
				System.out.println(str);
				System.out.println(iArgs);
				System.out.println(it315);//此处编译出错:InOut.java:13: local variable it315 is accessed from within inner class; needs to be declared final
			}
		}
	}
}
java中规定,内部类只能访问外部类中的成员变量,不能访问方法中定义的变量,如果要访问方法中的变量,就要把方法中的变量声明为final(常量)的,因为这样可以使变量全局化,就相当于是在外部定义的而不是在方法里定义的


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM