// // example of inner class (showing access to enclosing object). // import java.util.Vector ; class MyClass1 { String theName ; Vector things ; MyClass1(String theName) { this.theName = theName ; things = new Vector() ; } void addThing(int n) { things.addElement(new MySubClass(n)) ; } public String toString() { return "name = " + theName + "; things = " + things ; } // inner class for "subobjects" of MyClass1 objects class MySubClass { // within this class, have access to instance variables // of MyClass1 (theName) int n ; MySubClass(int n) { this.n = n ; } public String toString() { return n + " (in " + theName + ")" ; // theName references instance variable // in enclosing object } } } public class Nest1 { public static void main(String[] args) { MyClass1 o1 = new MyClass1("nums") ; MyClass1 o2 = new MyClass1("evens") ; for (int i = 0 ; i < 4 ; i++) { o1.addThing(i) ; o2.addThing(2*i) ; } System.out.println(o1) ; System.out.println(o2) ; } }