Java Inner Class

Variable Visibility In Anonymous Classes

/**
 * Class to test what sort of communication between
 * anonymous inner classes and the enclosing
 * method's local variables.
 * This experiment does not explore access to instance/static
 * variables and methods in the outer class.
 * @author Roedy Green
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Outer
   {

   static void osm()
      {
      int oi = 0;
      final int ofi = 1;

      // anonymous inner class
      ActionListener listener = new ActionListener()
         {

         public void actionPerformed( ActionEvent e )
            {
            // anonymous class method can directly
            // see local variable ofi of calling osm method.
            int ifi = ofi;

            // following are illegal, since only final local vars may be accessed
            // int ii = oi;
            // oi = i;
            } // end actionPerformed

         }; // end ActionListener

      } // end osm

   // the rules are the same for outer instance and static methods.
   void oim()
      {
      int oi = 0;
      final int ofi = 1;

      // anonymous inner class
      ActionListener listener = new ActionListener()
         {

         public void actionPerformed( ActionEvent e )
            {
            // anonymous class method can directly
            // see local variable ofi of calling osm method.
            int ifi = ofi;

            // following are illegal, since only final local vars may be accessed
            // int ii = oi;
            // oi = i;
            } // end actionPerformed

         }; // end ActionListener
      }
   } // end Outer


The rule is anonymous inner classes may only access final local variables of the enclosing method. Why? Because the inner class’s methods may be invoked later, long after the method that spawned it has terminated, e.g. by an AWT (Advanced Windowing Toolkit) event. The local variables are long gone. The anonymous class then must work with flash frozen copies of just the ones it needs squirreled away covertly by the compiler in the anonymous inner class object.

You might ask, why do the local variables have to be final? Could not the compiler just as well take a copy of non-final local variables, much the way it does for a non-finalparameters? If it did so, you would have two copies of the variable. Each could change independently, much like caller and callee’s copy of a parameter, however you would use the same syntax to access either copy. This would be confusing. So Sun insisted the local be final. This makes irrelevant that there are actually two copies of it.

The ability for an anonymous class to access the caller’s final local variables is really just syntactic sugar for automatically passing in some local variables as extra constructor parameters. The whole thing smells to me of diluted eau de kludge.


reference: http://mindprod.com/jgloss/nestedclasses.html


Why does Java prohibit static fields in inner classes?

class OuterClass {
 class InnerClass {
  static int i = 100; // compile error
  static void f() { } // compile error
 }
} 

8.1.2 Inner Classes and Enclosing Instances

An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).

static fields can only be declared in static or top level(top level classes: not nested, but possibly sharing a *.java file.) types

reference:  http://stackoverflow.com/questions/1953530/why-does-java-prohibit-static-fields-in-inner-classes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值