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

你可能感兴趣的:(Java语言)