Laboratory: Inner Classes
CSC 207 Algorithms and Object Oriented Design Spring 2011

Summary: In this lab, you will reinforce the properties of Java's inner classes.

Preparation

  1. Create a directory for this lab:
      mkdir somewhere/innerclasses
  2. Go to your new directory:
      cd somewhere/innerclasses
  3. Copy the files for this lab to your new directory:
      cp ~weinman/public_html/courses/CSC207/2011S/labs/code/innerclasses/Exercise1.java
      cp ~weinman/public_html/courses/CSC207/2011S/labs/code/innerclasses/Exercise2.java
      cp ~weinman/public_html/courses/CSC207/2011S/labs/code/innerclasses/Vector3D.java
    

Exercises

Exercise 1

Consider the following code (which you copied in the preparations):
Exercise1.java
public class Exercise1 {

  private int i = 1;
  private static int j = 42;

  static class Inner {

    private int x = i + j;

  }

}
  1. Is the declaration of x legal? Why or why not?
  2. Compile the code to verify your prediction.
  3. What if we made class Inner non-static?
  4. Compile the code to verify your prediction.
  5. What do we call such classes when they are declared non-static? static?
  6. Which type would allow a reference to Exercise1.this.i somewhere in Inner1?
  7. What is the main difference between the objects of these two types of classes? How is this use of the static similar to other places you have seen it used (e.g. variables and methods)? Confirm your explanation with a neighbor.
  8. Restore the static modifier to class Inner. Say we added the following method at line 9.
        private int getX() { return x; }
    Is this a legal declaration? Does the method need to be static? Why or why not?
  9. Add the method and compile to verify your prediction.
  10. If your hypothesis was incorrect, figure out why. Confirm with the instructor or a neighbor.

Exercise 2

Consider the following code:
Exercise2.java
public class Exercise2 {
  
  private int i;
  private static int j = 42;
  
  public int fiddle() {

    Inner1 obj = new Inner1(); // Default constructor

    i = Inner1.x;

    return i + j;
  }

  private static class Inner1 {
      
    private static int x = 207;

  }

}
  1. Is the access of the private member of the private nested class i = Inner1.x; legal?
  2. Compile the program to test your prediction.
  3. What do you suppose is the reason for this behavior?

Exercise 3

  1. Study the class Vector3D and its inner class, which you copied to your directory at the outset of this lab, to make sure you understand its intended behavior.
  2. Upon inspection, do you expect the code to compile?
  3. Compile Vector3D.java. What happens? Try to come up with an explanation.
  4. Change line 18 to the following:
      private class VectorIterator<AnyType2> implements Iterator<AnyType2> {
  5. Now recompile the code. What happens? Why?
  6. Modify the return type of next() to resolve this problem.
  7. Recompile again. What is different from the first time you compiled (something subtle)? Does this make marginally more sense?
  8. Now change line 18 to the following:
      private class VectorIterator implements Iterator<AnyType> {
    and restore the return type of next() to AnyType.
  9. Recompile once again. Now what happens?
  10. Why can't we re-parameterize VectorIterator the way we first tried to? Be prepared to reflect on this.

Exercise 4

  1. Complete the hasNext and next routines of VectorIterator.
  2. Test your implementation using the program in the main method of Vector3D.

For those with extra time

Created: Jerod Weinman, 12 January 2009
Modified: Jerod Weinman, 18 January 2011
Copyright © 2009-2011 Jerod Weinman.
CC-BY-NC-SA
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .