Answers to Questions and Exercises: Nested Classes
Questions
-
Question: The program
Problem.java
doesn't compile. What do you need to do to make it compile? Why?Answer: Delete
static
in front of the declaration of theInner
class. An static inner class does not have access to the instance fields of the outer class. SeeProblemSolved.java
. -
Use the Java API documentation for the
Box
class (in thejavax.swing
package) to help you answer the following questions.-
Question: What static nested class does
Box
define?Answer:
Box.Filler
-
Question: What inner class does
Box
define?Answer:
Box.AccessibleBox
-
Question: What is the superclass of
Box
's inner class?Answer:
[java.awt.]Container.AccessibleAWTContainer
-
Question: Which of
Box
's nested classes can you use from any class?Answer:
Box.Filler
-
Question: How do you create an instance of
Box
'sFiller
class?Answer:
new Box.Filler(minDimension, prefDimension, maxDimension)
-
Exercises
-
Exercise: Get the file
Class1.java
. Compile and runClass1
. What is the output?Answer:
InnerClass1: getString invoked.
InnerClass1: getAnotherString invoked. Exercise: The following exercises involve modifying the class
DataStructure.java
, which the section Inner Class Example discusses.Define a method named
print(DataStructureIterator iterator)
. Invoke this method with an instance of the classEvenIterator
so that it performs the same function as the methodprintEven
.Hint: These statements do not compile if you specify them in the
main
method:DataStructure ds = new DataStructure(); ds.print(new EvenIterator());
The compiler generates the error message, "non-static variable this cannot be referenced from a static context" when it encounters the expression
new EvenIterator()
. The classEvenIterator
is an inner, non-static class. This means that you can create an instance ofEvenIterator
only inside an instance of the outer class,DataStructure
.You can define a method in
DataStructure
that creates and returns a new instance ofEvenIterator
.Invoke the method
print(DataStructureIterator iterator)
so that it prints elements that have an odd index value. Use an anonymous class as the method's argument instead of an instance of the interfaceDataStructureIterator
.Hint: You cannot access the private members
SIZE
andarrayOfInts
outside the classDataStructure
, which means that you cannot access these private members from an anonymous class defined outsideDataStructure
.You can define methods that access the private members
SIZE
andarrayOfInts
and then use them in your anonymous class.Define a method named
print(java.util.Function<Integer, Boolean> iterator)
that performs the same function asprint(DataStructureIterator iterator)
. Invoke this method with a lambda expression to print elements that have an even index value. Invoke this method again with a lambda expression to print elements that have an odd index value.Hint: In this
print
method, you can step though the elements contained in the arrayarrayOfInts
with afor
expression. For each index value, invoke the methodfunction.apply
. If this method returns a true value for a particular index value, print the element contained in that index value.To invoke this
print
method to print elements that have an even index value, you can specify a lambda expression that implements the methodBoolean Function.apply(Integer t)
. This lambda expression takes oneInteger
argument (the index) and returns aBoolean
value (Boolean.TRUE
if the index value is even,Boolean.FALSE
otherwise).Define two methods so that these statements print elements that have an even index value and then elements that have an odd index value:
DataStructure ds = new DataStructure() // ... ds.print(DataStructure::isEvenIndex); ds.print(DataStructure::isOddIndex);
Hint: Create two methods in the class
DataStructure
namedisEvenIndex
andisOddIndex
that have the same parameter list and return type as the abstract methodBoolean Function<Integer, Boolean>.apply(Integer t)
. This means that the methods take oneInteger
argument (the index) and return aBoolean
value.
Answer: See the file
DataStructure.java
.