/*
* Outer.java Adapated
from: Lewis/Loftus
* Represents a class that encapsulates an inner class.
*/
public class Outer
{
private int num;
private Inner in1, in2;
public Outer() // Constructor initializes all data
{
num = 9876;
in1 = new Inner ("Half of the problem is 90%
mental.");
in2 = new Inner ("Another deadline. Another
miracle.");
}
public void changeMessages()
{
// Changes the messages in
the Inner objects (directly).
in1.message = "Life is uncertain. Eat
dessert first.";
in2.message = "One seventh of your life
is spent on Mondays.";
}
public String toString()
{
return in1 + "\n" + in2;
}
// Represents an inner class.
private class Inner
{
public String message;
public Inner (String str)
{
message = str;
}
public String toString()
{
num++; // Access
outer class variables directly
return message +
"\nOuter num = " + num;
}
}
}