Summary of Class Presentation for June 25, 1999 Topc: Inner Classes Java supports a capability for nesting one class inside of another. The wrapper class is called an outer class. The nested class is called an inner class. Inner classes differ from other classes in two ways: 1. they and their object variables are accessible only in the outer class and all inner classes of that outer class 2. when you set up an object of an inner class, you need to associate that object with an already existing object of the outer class. If a is the outer class and b is the inner class, you could write b sam = a.new b(); The main advantage of inner classes is the capability to restrict where they and their objects can be used. If you make an inner class private, that inner class and its objects can be used only in the outer class and that inner class itself. They are not accessible in other inner classes of that outer class. Inner classes may also be nested within a method. In this case, they are accessible only to that method. Inner classes may be nested within any set of curly brackets. For example, an inner class could appear within the else clause of an if. Anonymous inner classes ( those without any name) can be used also. When an anonymous class is set up, it must be on the same statement that creates an object of that class and either returns that object or assigns it to an object variable. That object of the anonymous class can be used as any other object. However, that statement which creates it is the only way an object of that anonymous class can be created. hence, it makes no sense for the anonymous class to have class methods since they can never be used ( the class has no name). You should not read the pages in the textbook from 288 - 298. These pages show how the link between an object of the inner class and the creating object of the outer class can be used. They are confusing and we will not use any of this material during this course. You should, however, read the pages starting with Why Inner Classes: Control Frameworks on page 298. The examination next Wednesday will consist of twelve small sample programs. You will be asked to either write a few lines of code in a specific spot in one of the samples, or to explain why one or more statements within a sample program are not legal.