- public class Test1 {
- class InnerBBB {
- int b=100;
- }
- public static void main(String[] args) {
- Test1 test1 = new Test1();
- AAA innerAAA = test1.method();//如果innerBBB不声明为final,方法执行完innerBBB出栈,就无法访问成员变量b的值
- innerAAA.run();
- }
- public AAA method() {
- final InnerBBB innerBBB = new InnerBBB();//声明为final
- class InnerAAA implements AAA {
- @Override
- public void run() {
- System.out.println("访问内部类InnerBBB的成员变量b的值是"+innerBBB.b);
- }
- }
- InnerAAA innerAAA = new InnerAAA();
- // innerAAA.run();
- return innerAAA;
- }
- }
- interface AAA {
- void run();
- }
复制代码 |