Thursday, September 9, 2010

Code to Understand Objective Method-Local Inner Classes

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)


Define an inner class within a method


/*
Code declares a class, MyOuter2, with one method, doStuff(). But inside doStuff(), another class, MyInner, is declared, and it has a method of its own, seeOuter(). The code is completely useless, however, because it never instantiates the inner class! Just because you declared the class doesn’t mean you created an instance of it.
*/

class MyOuter2 {
     private String x = "Outer2";

     void doStuff() {
        class MyInner {
           public void seeOuter() {
             System.out.println("Outer x is " + x);
           }
// close inner class method
        }
// close inner class definition
      }
// close outer class method doStuff()

     public static void main (String[] args){
        MyOuter2 outer = new MyOuter2();
outer.doStuff();
     }

}
// close outer class







To use the inner class you must make an instance of it somewhere within the method but below the inner class definition 









/*
Code declares a class, MyOuter2, with one method, doStuff(). But inside doStuff(), another class, MyInner, is declared, and it has a method of its own, seeOuter(). The code is completely useless, however, because it never instantiates the inner class! Just because you declared the class doesn’t mean you created an instance of it.

When outer.doStuff() is invoked, doStuff() will invoke seeOuter() because of inner.seeOuter() statement before the close of doStuff()
*/

class MyOuter2 {
     private String x = "Outer2";

     void doStuff() {
        class MyInner {
           public void seeOuter() {
             System.out.println("Outer x is " + x);
           } // close inner class method
        } // close inner class definition
        MyInner inner = new MyInner(); // this line must come after the inner class declaration
        inner.seeOuter();  // tie the inner class to it's method
      } // close outer class method doStuff()

     public static void main (String[] args){
        MyOuter2 outer = new MyOuter2();
outer.doStuff(); 
     }

} // close outer class









What a Method-Local Inner Object Can and Can’t Do




/*
A method-local inner class can be instantiated only within the method where the inner class is defined. However, the inner class object cannot use the local variables of the method the inner class is in. Why not?

The local variables of the method live on the stack, and exist only for the lifetime of the method. You already know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren’t guaranteed to be alive as long as the method-local inner class object, the inner class object can’t use them. Unless the local variables are marked final! The following code attempts to access a local variable from within a method-local inner class.
*/

class MyOuter2 {
   private String x = "Outer2";
   void doStuff() {
     String z = "local variable";
     class MyInner {
       public void seeOuter() {
         System.out.println("Outer x is " + x);
         System.out.println("Local variable z is " + z);  // Won't Compile!
       } // close inner class method
     }   // close inner class definition
     MyInner inner = new MyInner();
     inner.seeOuter();
   }     // close outer class method doStuff()

   public static void main(String[] args){
      MyOuter2 outer = new MyOuter2();
      outer.doStuff();
   }
}        // close outer class



No comments:

Post a Comment