Trail: Learning the Java Language
Lesson: Classes and Objects
Home Page > Learning the Java Language > Classes and Objects

Answers to Questions and Exercises: Nested Classes

Questions

  1. 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 the Inner class. An static inner class does not have access to the instance fields of the outer class. See ProblemSolved.java.

  2. Use the Java API documentation for the Box class (in the javax.swing package) to help you answer the following questions.

    1. Question: What static nested class does Box define?

      Answer: Box.Filler

    2. Question: What inner class does Box define?

      Answer: Box.AccessibleBox

    3. Question: What is the superclass of Box's inner class?>

      Answer: [java.awt.]Container.AccessibleAWTContainer

    4. Question: Which of Box's nested classes can you use from any class?

      Answer: Box.Filler

    5. Question: How do you create an instance of Box's Filler class?

      Answer: new Box.Filler(minDimension, prefDimension, maxDimension)

Exercises

  1. Exercise: Get the file Class1.java. Compile and run Class1. What is the output?

    Answer: InnerClass1: getString invoked.
    InnerClass1: getAnotherString invoked.

« PreviousTOC

Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.

Previous page: Questions and Exercises: Nested Classes