CITS2210 Object-Oriented Programming
Topic 10
Java: Nested and Inner Classes
Summary: Nested and inner classes allow classes do be defined within other classes, and even within bodies of methods. They enable certain styles of programming that are awkward otherwise.
1 What are nested and inner classes?
A nested class is a class defined inside another class.
public class OuterClass {
class NestedClass {
}
}
Nested class declarations can appear in classes and interfaces along with the more standard variable declarations and method declarations.
– Like other declarations, they can have modifiers like public, static, etc.
– A non-static nested class is called an inner class.
You can always write separate classes instead of using nested classes, but they are still useful for the following reasons.
– They allow “helper” classes to be logically grouped with a main class.
– They allow improved encapsulation since nested classes can access private variables, and can be private themselves.
– They allow code to be better organized, with related code appearing in one place.
2 Static nested classes
Static nested classes are “attached” to the outer class.
– They can only access static variables and methods from the outerclass directly.
– As such, static nested classes can usually be easily moved to become top-level classes – the nesting is only used for organization.
– Static nested classes are can be accessed via the outer class, just like static methods and class variables (assuming that they are not private). public class Outer {
public static class StaticNested {
} }
Outer.StaticNested nested = new Outer.StaticNested();
3 Inner classes
Inner classes are attached to a particular instance of the outer class.
– So, each instance of the inner class is also attached to this instance of the outer class.
– To create instances of an inner class you need an instance of the outer class so that you can use “new” with the inner class in that instance.
– Instances of inner classes can directly access all the methods and instance variables of the enclosing class for the instance they are attached to.
public class Outer { private int instanceVar;
public class Inner { public int getOuter() { return instanceVar; } } }
Outer outer = new outer(); Outer.Inner inner = outer.new Inner(); int i = inner.getOuter;
4 Extended example of an inner class public class DataStructure { private final static int SIZE = 15; private int[] arrayOfInts = new int[SIZE];
public DataStructure() { for (int i = 0; i < SIZE; i++) { arrayOfInts[i] = i; //fill the array } }
public void printEven() { //print out values of even indices of the array
}
private class InnerEvenIterator { private int next = 0; // Begin at the start of the array.
public boolean hasNext() { return (next <= SIZE - 1); }
public int getNext() { int retValue = arrayOfInts[next]; next += 2; return retValue; } }
public static void main(String s[]) { DataStructure ds = new DataStructure(); InnerEvenIterator iterator = ds.new InnerEvenIterator(); while (iterator.hasNext()) { System.out.println(iterator.getNext() + " "); } } }
5 Local and anonymous classes
Sometimes an inner class is created specifically to be used in one particular place.
– In these cases, it makes sense to define the class where it is used rather than separately as a part of the outer class.
– A local class is an inner class that is defined a method body.
– An anonymous class is a class that is defined at the exact point where it is used, i.e., where an instance of the class is created.
– Local classes may be used many times within the the method where they are defined, while anonymous classes are only used once. interface IntRunnable { int run(); } public class AnonEx {
public static int callRun(IntRunnable runnable) { runnable.run(); }
public static void main(String argv[]) { callRun(new IntRunnable() { // Anonymous class public int run() { return 3 * 5; } }); // Close class then call to callRun } }
6 What use are anonymous classes?
Anonymous classes turn out to be particularly useful when creating complex objects from a collection of simpler ones, each with their own methods.
– It is also often useful for “glue code”.
– These styles of programming largely borrow from functional programming, and are generally referred to as “higher order”.
– Increasingly this style is influencing object-oriented programming.
– C# in particular has been highly influenced.
7