Sample Code - University of Sunderland
Download
Report
Transcript Sample Code - University of Sunderland
An Advanced Code Pattern:
Inner Classes
CSE301
University of Sunderland
Harry R. Erwin, PhD
Half Lecture
Why Bother with Inner Classes?
• Java (like C++) tends to separate class definitions from where
they are used.
• Java also tends to produce an exponential explosion in the
number of classes in the system. This makes the architecture
hard to understand and manage.
• Inner classes hide (encapsulation!) the extra helper classes
needed to implement the system.
• Inner classes also localize the definition of these helper
classes to exactly where they are used.
• Particularly important in graphical user interfaces, where
events are usually handled by inner classes.
Inner Classes
• Static member classes (and interfaces) are defined
as static members of another class.
• Member classes are instantiated within another
class and have access to the fields of the enclosing
class.
• Local classes are defined within a code block for
use there.
• Anonymous classes are designed to be used as
temporary variables in method calls without being
given names.
Static Member Classes and
Interfaces
• class or interface defined as a static member of another class.
class Foo {
static class Bar {…}
}
• Behaves like a top-level class but can access the static
members of the containing class, including private members.
• Use like ‘friend’ in C++, frequently serving as a helper class.
A static member interface can be defined to document how a
class is to be accessed by another class.
Member Classes
• A class (not interface) defined as a member of another
class.
class Foo {
class Bar {…}
}
• Can access the members of the containing class instance.
• Cannot contain static fields, methods, or classes (except
static constant fields).
• Use like ‘friend’ in C++, often to implement a static
member interface or a standard interface.
• For example, this can be used to define an Iterator for a
user-defined Collection class.
Local Classes
• Defined within a block of Java code (in a method or
initializer) and visible only there.
• Similar to member classes because the code is in a class.
Local variables, arguments, and exception parameters it
uses must be declared final so it can keep its own copies
and have them be current.
• Often used to define an implementation of an interface:
Iterator getIterator() {
class myIterator implements Iterator
{define here how myIterator works}
return new myIterator();
}
Anonymous Classes
• A local class without a name. Often used as an
adaptor or a method argument.
• Combines class definition syntax with object
instantiation syntax. No constructor. Use an
instance initializer block instead.
• An expression, so it can be part of a larger
expression, such as a method invocation.
new java.util.Iterator() {class definition that
implements an interface or extends a class}
new MyIterator(arg list) {another class defn.}
Initialization Blocks
• static {code} is used to initialize static member fields
for a class. These are run in order of appearance in the
class definition.
• {code} is used to do common initialization of instance
fields for all constructors. These are also run in order
of appearance before the constructor runs.
• This does imply initialization code may run in an
unexpected order!
• Used to support anonymous inner classes, which
cannot have constructors (as they have no name).
Example of Initialization
public class Foo {
public Foo() {
bar = 2;
}
static { baz = 2;}
{ bar = 1;}
int bar = 0;
static int baz = 1;
}
What are the values of bar and baz when the
class instance is finally initialized?
Defining a Static Member
Interface
public class LinkedStack {
public static interface Linkable {
public Linkable getNext();
public void setNext(Linkable node);
}
Linkable head;
public void push(Linkable node) {…}
public Object pop() {…}
}
From Flanagan, Java in a Nutshell, 4th edition, O’Reilly, 2002.
Using that Interface
Class LinkableInteger implements LinkedStack.Linkable {
int I;
public LinkableInteger(int I) {this.I = I;}
LinkedStack.Linkable next;
public LinkedStack.Linkable getNext()
{ return next;}
public void setNext(LinkedStack.Linkable node)
{ next = node;}
}
Note the syntax for referring to the interface outside the class.
From Flanagan, Java in a Nutshell, 4th edition, O’Reilly, 2002.
A Member Class
public class LinkedStack {
public static interface Linkable {…}
private Linkable head;
class members …
public java.util.Iterator iterate() {
return new myIterator();
}
protected class myIterator implements java.util.Iterator {
Linkable current;
public myIterator() { current = head;}
Iterator definition …
}
}
From Flanagan, Java in a Nutshell, 4th edition, O’Reilly, 2002.
A Local Class
// returns an Iterator object
Public java.util.Iterator iterate() {
class myIterator implements java.util.Iterator {
Linkable current;
public myIterator() { current = head;}
provide the member functions required
to implement an Iterator …
}
return new myIterator();
}
This encapsulates an Iterator in the class being iterated.
From Flanagan, Java in a Nutshell, 4th edition, O’Reilly, 2002.
An Anonymous Class
Public java.util.Iterator iterate() {
return new java.util.Iterator {
Linkable current;
{ current = head;} // instance initializer
// No constructor!
various member functions implementing Iterator
…
}; // semicolon terminates the return statement
}
From Flanagan, Java in a Nutshell, 4th edition, O’Reilly, 2002.