



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Overview
Top-level vs. inner & nested
Iterator example Used inside outer class
Syntax Uses for GUIs
Inner / Nested Classes
public class MyOuterClass { public class MyInnerClass { … } static public class MyNestedClass { … } Iterator iterator( ) { return new Iterator( ) { … } } }
Inner Classes
Class defined in scope of another class May be named or anonymous
Outer & inner class can directly access each other’s fields & methods (even if private)
Inner Class Link To Outer Class
Has association to an instance of outer class Must be instantiated with an enclosing instance Is tied to outer class object at moment of creation (can not be changed)
MyList MyList
MyIterator MyIterator MyIterator
Inner Classes
Private helper classes Logical grouping of functionality Data hiding Linkage to outer class Inner class object tied to outer class object
Iterator for Java Collections ActionListener for Java GUI widgets
(Problematic) MyIterator Design
public class MyIterator implements Iterator { private MyList list; private int pos; MyIterator(MyList list) { this.list = list; pos = 0; } public boolean hasNext() { return pos < list.size; } public Object next() { return list.a[pos++]; } … }
MyIterator Design
Need to maintain reference to MyList Need to access private data in MyList
Define MyIterator as inner class for MyList Instance of MyIterator tied to instance of MyList MyIterator methods can access private MyList fields MyIterator objects can iterate through elements of MyList
Iterable Interface
Instantiating Inner Class
Outer class method returns instance of inner class Used by Java Collections Library for Iterators
public class MyList { public class IC implements Iterator { … } public Iterator iterator() { return new IC(); // creates instance of IC } } MyList m = new MyList(); Iterator it = m.iterator();
Using Inner Class Inside Outer Class
public class OC { // outer class private int x = 2; private class IC { // inner class private int y = 4; private int getSum() { return x + y; } } void bar( ) { IC z = new IC( ); // create new IC assoc. w/ this z.getSum( ); // treat z like normal object } }
Accessing Outer Scope
public class OC { // outer class int x = 2; private class IC { // inner class int x = 6; private void getX() { // inner class method int x = 8; System.out.println( x ); // prints 8 System.out.println( this.x ); // prints 6 System.out.println( OC.this.x ); // prints 2 } } }
Anonymous Inner Class
Inner class without name Defined where you create an instance of it In the middle of a method Returns an instance of anonymous inner class Useful if the only thing you want to do with an inner class is create instances of it in one location
new ReturnType( ) { // unnamed inner class body of class… // implementing ReturnType } ;
Anonymous Inner Class
public class MyList { public Iterator iterator( ) { return new Iterator( ) { // unnamed inner class … // implementing Iterator } ; } } MyList m = new MyList( ); Iterator it = m.iterator( );