CS351 Spring 2004 Lab 8

Inner Classes, a Review


Basics

When we are talk about inner classes, we are referring to class which are defined inside of another class. The inner class is said to be enclosed by the outer class.
	class Outer{
		class Inner{}
	}
Inner classes allow you to organize your classes into logical scopes.

Two Kinds

There are two distinct kinds of inner classes, static and non-static.

Non-static:

	class Outer
	{
		class Inner{}
	}
Static:
	class Outer
	{
		static class Inner {}
	}

Static Inner Classes

First, accept the fact that in terms of inner classes the keyword static means something rather different than it does in other contexts. I don't claim that it makes sense, but it's the way it is.

In terms of inner classes, static is used to denote that the class is simply logically related to the class that encloses it. A class E enclosing class I, simply means that outside of E, you have to refer to I as E.I . This only effecting naming of classes and how they are referred to, and does not effect the scope of the code inside of either class.

An example of where these may come into play if one were implementing a linked list in Java. The nodes for the linked list have no knowledge of the linked list itself, but the class for the nodes is logically inside of the linked list. So a static inner class would be appriorate.

Non-static Inner Classes

A non-static inner class is associated with an instance of the enclosing class. The inner class has all the methods and variables of the enclosing class (including private) within its scope.
	class Outer
	{
		private int x;
		class Inner{
			private int i;
			Inner()
			{
				i = x;
			}
		}
	}
In this example the inner class is directly using the variables of its enclosing class. Also note that enclosing class can access the private members of its enclosed class, though it needs a reference to it (as it may have multiple instances of the same inner class associated with it).
	//In Outer somewhere
	Inner inner_class = new Inner();
	inner_class.i = 5;
By default an inner class is associated with the class that instantiated it, but you can specified which class it is to be associated with:
	//In Outer somewhere (though doesn't necessarily need to be)
	Outer other = new Outer();
	Inner inner_class = other.new Inner();
The new inner class will be associated with the class other.

In the case that the inner class has variables that shadow ones in the enclosing class, we have to refer to the enclosing object explicitly:

	class Outer
	{
		private int x;
		class Inner{
			private int x;
			Inner()
			{
				x = Outer.this.x;
			}
		}
	}
Exercise

You are to write a short program that demonstrates the usage of inner classes. it should have a class A with classes B and C enclosed. B is to be non-static, C is to be static. Also have a class D with static class E enclosed within it. Have each class have a basic "Hello I'm Class x" method on it.

Your main class should be in class D, and it should just instantiate class A. Your constructor for class A should instantiate instances of class B, C, and E. Demonstrate in your code that class A's private members are accessible from B.

The goal of this exercise to make the scoping issues of inner classes clear. When writing it, experiment to see what variables you can access from different classes.

Due:
For Monday, by 5:00 PM Wednesday
For Wednesday, by 5:00 PM Friday