SDSU CS 596 Java Programming
Fall Semester, 1998
Nested Classes Addendum
To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 13-Nov-98

Contents of Doc 19, Nested Classes Addendum


References


Java in a Nutshell , 2 nd Ed., David Flanagan, O'Reilly, 1997, Chapter 5.

The Java Compiler

Doc 19, Nested Classes Addendum Slide # 2

Nested Classes Addendum


In class on Oct 28, there were several unanswered questions and there was an error in Doc 17. This document addresses the questions and corrects the error. The error has been corrected in the on-line version of Doc 17.

You can nest Top-Level Nested classes


This example shows that you can nest Top-level Nested classes. The code below does compile.

class A {
   public static class B {
      public static class C {
      }
   }
}

Local Classes must be Declared Before Being Used


Using a local class before declaring it is a compile error. The line "Local compileError;" results in a compile error.

public class TopLevel  {
   
   public void test(  ) {
      Local compileError;
      class Local  {
      }
      Local ok = new Local();
   }


Doc 19, Nested Classes Addendum Slide # 3

Inner Classes and Inheritance


This example, which spans three slides, shows how inner classes can reference its parent's class and its enclosing classes parent's class. While an inner class can access its enclosing classes private fields and methods, it can not access the private fields and methods of parent class of its enclosing classes. An inner class has the same access to the parent class of its enclosing class as the enclosing class itself. In the example there are six classes: A, B, InnerA, TopLevel, InnerTopA and InnerTopB. In the diagram below, each class is represented by a box. Inner classes are shown as boxes inside of boxes. Lines, reading from top (parent) to bottom (child), show inheritance. InnerTopB has the same access to A's fields and methods as TopLevel.


class A {
   protected String name = "A";
   
   public class InnerA {
      protected String name = "InnerA";
   }
}
class B {
   protected String name = "B";
}

Doc 19, Nested Classes Addendum Slide # 4
Inner Classes and Inheritance Example Continued

The print method in InnerTopA shows how to access fields of the different classes from InnerTopA. Note A.this.name works because A is the parent of TopLevel. InnerTopA has the same access of A's fields and methods as TopLevel. TopLevel.super.name is not valid in InnerTopA.

class TopLevel extends A {
   protected String name = "TopLevel";
   
   public void print() {
      InnerTopA topA = new InnerTopA();
      InnerTopB topB = new InnerTopB();
      System.out.println( "InnerTopA");
      topA.print();
      System.out.println( "InnerTopB");
      topB.print();
   }
   
   public class InnerTopA extends A.InnerA {
      protected String name = "InnerTopA";
      
      public void print() {
      
         System.out.println( "name " + name );
         System.out.println( "super.name " + super.name );
         System.out.println( "TopLevel.this.name  " + 
                                       TopLevel.this.name );
         System.out.println( "A.this.name  " + A.this.name );
      }
   }

Doc 19, Nested Classes Addendum Slide # 5
Inner Classes and Inheritance Example Continued
   public class InnerTopB extends B {
      protected String name = "InnerTopB";
      
      public void print() {
      
         System.out.println( "name " + name );
         System.out.println( "super.name " + super.name );
         System.out.println( "TopLevel.this.name  " + 
                                          TopLevel.this.name );
         System.out.println( "A.this.name  " + A.this.name );
         System.out.println( "B.this.name  " + B.this.name );
      }
   }
}
public class Test  {
   public static void main( String args[] ) {
      TopLevel example = new TopLevel();
      example.print(); 
   }
}
Output
InnerTopA
name InnerTopA
super.name InnerA
TopLevel.this.name TopLevel
A.this.name A
InnerTopB
name InnerTopB
super.name B
TopLevel.this.name TopLevel
A.this.name A
B.this.name B

Doc 19, Nested Classes Addendum Slide # 6

One Inner Class Extending Another Inner Class


If inner class InnerTopA extends another inner class, then either TopLevel is the enclosing class for both inner classes or TopLevel extends the enclosing class of the other inner class.

Same Class Encloses Both Inner Classes
class TopLevel {
   public class InnerTopA extends InnerTop {
   }
   
   public class InnerTop {
   }
}
Enclosing Class extends Other Enclosing Class
class A {
   public class InnerA {
   }
}
class TopLevel extends A {
   public class InnerTopA extends A.InnerA {
      }
   }

This Does Not Work
class TopLevel {
   public class InnerTopA extends A.InnerA {
      }
   }

Listen Here!Q-nov5 3mins Doc 19, Nested Classes Addendum Slide # 7

Inner Class Extending Its Enclosing Class


An inner class can extend its enclosing class.

I can see no reason for doing this, but it can be done. It is not a security problem, so why clutter the compiler with a check to exclude this situation?

class TopLevel {
   protected String name = "TopLevel";
   
   public class InnerTop extends TopLevel {
      protected String name = "InnerTop";
      
   }
}

Doc 19, Nested Classes Addendum Slide # 8

Error in Doc 17, slide 12


Doc 17, slide 12 contained the following two incorrect or misleading statements:

A local class can use any final field , variable or method parameters that are visible from the scope in which it is defined
A local class cannot:
Use any field, variable or method parameter that are visible from the scope in which it is defined and is not final

They should read:

A local class can use any final variable or method parameters that are visible from the scope in which it is defined
A local class cannot:
Use any variable or method parameter that are visible from the scope in which it is defined and is not final
The error has been corrected in the on-line version of Doc 17. The following slide shows an example of what is accessible from a local class.

Listen Here!Q-nov5 5mins Doc 19, Nested Classes Addendum Slide # 9

Data Accessible to Local Class

h and f are not accessible to the local class "Local" as they are not final. All other fields, local variables and arguments shown are accessable in "Local"

class A {   protected String a = "A";}
class B {   protected String b = "B"; }
public class TopLevel extends A {
   private String c = "C";
   public static String d = "D";
   
   public void print( final String e, String f ) {
      final String g = "G";
      String h = "H";
      class Local extends B {
         String i = "I";
         
         public void print() {
            System.out.println( a );  //inherited by enclosing class
            System.out.println( b );  //inherited by this class
            System.out.println( c );  //field of enclosing class
            System.out.println( d );  //field of enclosing class
            System.out.println( e );  //final local argument
            System.out.println( g );  //final local varaible
            System.out.println( i );  //field of this class
         }
      }
      Local example = new Local();
      example.print();
   }
}

Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

visitors since 30-Oct-98