Friday, March 20, 2009

Inner Classes

Inner Classes gives us a good introductions. Inner classes come in four flavors:: Static member class, Member class, Local class, Anonymous class. And the advantages of inner classes can be divided into three categories: an object-oriented advantage, an organizational advantage, and a call-back advantage.

The OO advantage is not well known for me compared with call-back advantage. One good example is to separate search algorithm as inner class in Tree class. Another is to nest MuListElement class inside BeastGetActiveMuRespMsg - composition relationship in UML.


public class BeastGetActiveMuRespMsg {
private int retCode;
private MuListElement[] muList;
....
public static class MuListElement {
private String macAddress;
private String ipAddress;
...
}
}

A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

Like a static member class, a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.

No comments: