Hi.. I have a query regarding accessibility of top level class from member inner class. I have just read the reason why local or anonymous inner classes can access only final variables.The reason being JVM handles these two classes as entirely different classes and so, if value of variable in one class changes, it can't be reflected at run time in another class file..

Then, my question is that how an inner member class (non-static) can have access to members to members of top level class, as JVM is still treating these two classes as different class files? If value of a member variable of top level class changes, how will it possible to reflect in class file of inner class at runtime?

Thanks & Regards,

Sumit

Recommended Answers

All 5 Replies

See page 182 of the Java Language Specification, 3rd edition (may be a different page in other editions).

Basically, an inner class contains an implicit reference to the outer class instance that creates it, thus preventing that instance from going out of scope (and thus getting garbage collected).
However, method local variables do go out of scope unless they're declared final, in which case they're preserved between method calls.
For an instance of a method local inner class to be able to refer to those method local variables after the method goes out of scope (say, if you return the instance from the method), those method local variables thus have to be declared final else they will no longer exist when needed by the inner class instance that refers to them.

method local variables do go out of scope unless they're declared final, in which case they're preserved between method calls.

Sorry, I don't think that's quite right. final variables do go out of scope, and a new final variable is definitely assigned for each invocation of the method. The same final variable may have different values in different invocations. Inner classes are given their own copy of any final variables they need. They must be final to avoid any ambiguity about which value should be given to the copy.

public class Demo {

   static void doFinal(int i) {
      final int f = i; // different value in each invcation
      new javax.swing.Timer(2000, new java.awt.event.ActionListener() {
         // anonymous inner class
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            System.out.println("f is " + f); // ref to final variable
         }
      }).start();
   }

   public static void main(String[] args) {
      doFinal(1);
      doFinal(2);
      try {
         Thread.sleep(3000); // allow Timers to do their stuff
      } catch (InterruptedException e) {
      }
   }

}

yes, they can get new values during new method calls, but will be preserved for the instance of the method local inner class instance created by the previous call.

A quick look at the generated code reveals the mechanism - if the inner class accesses a final variable "fred" of the enclosing method then the inner class is given a private final variable called "val$fred" whose value is a copy of fred's.
fred (which was allocated on the stack when the method was invoked) is freed up when the method terminates and is popped of the stack, but val$fred's lifetime is the same as the inner class instance to which it belongs.

Hey, thnx all... I got it now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.