<< Chapter < Page | Chapter >> Page > |
Comments | |
package listFW.factory;
import listFW.*;public class InnerCompListFact implements IListFactory {
public static final InnerCompListFact Singleton= new InnerCompListFact();
private InnerCompListFact() {} |
|
private final static IListAlgo ToStrHelp = new IListAlgo() {
public Object emptyCase(IMTList host, Object... acc) {return acc[0] + ")";}
public Object nonEmptyCase(INEList host, Object... acc) {return host.getRest().execute(this,
acc[0]+ ", " + host.getFirst());
}}; // PAY ATTENTION TO THE SEMI-COLON HERE! |
|
private final static IMTList MTSingleton = new IMTList (){
public Object execute(IListAlgo algo, Object... inp) {return algo.emptyCase(this, inp);
}public String toString() {
return "()";}
}; // PAY ATTENTION TO THE SEMI-COLON HERE! |
|
public IMTList makeEmptyList() {
return MTSingleton;} |
|
public INEList makeNEList(final Object first,
final IList rest) {return new INEList() {
public Object getFirst() {return first;
}public IList getRest() {return rest;
}public Object execute(IListAlgo algo,
Object... inp) {return algo.nonEmptyCase(this, inp);
}public String toString() {
return (String)rest.execute(ToStrHelp,"(" + first);
}};
}} |
Note how the code inside the anonymous inner class references first and rest of the parameter list.
first and
rest are said to be in the
closure of the anonymous inner class.
Here is an important Java syntax rule: For an local inner class defined inside of a method to access a
local variable of the method, this local variable must be declared as
final . |
Click here to download the code of all of the above .
Besides fields and methods, a Java class can also contain other classes. And just like a field or method defined inside of a class, a class defined inside of another class can be static or non-static. Here is the syntax.
class X {
// fields of X ...// methods of X ...
/*** named class Y defined inside of class X:
*/[public|protected|private][static][final][abstract]class Y [extends A][implements B]{
// fields of Y ...// methods of Y ...
// classes of Y ...}
}
When an embedded class is defined as
static
, it is called a
nested class .
The
members
(i.e. fields, methods, classes) of a (static) nested class can access only static members of the enclosing class.
When an embedded class is non-
static
, it is called an
inner class . The members of an inner class can access ALL members of the enclosing class. The enclosing class (and its enclosing class, if any, and so on) contains the environment that completely defines the inner class and constitutes what is called the
closure of the inner class. As all functional programmers should know, closure is a powerful concept. One of the greatest strength in the Java programming language is the capability to express closures via classes with inner classes. We shall see many examples that will illustrate this powerful concept in other modules.
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?