CSC 207 Grinnell College Spring, 2012
 
Algorithms and Object-Oriented Design
 

Inner Classes in Java

Summary

This lab reinforces properties of Java's inner classes.

Acknowledgment

Much of this lab comes from materials by Jerod Weinman.

Preparation

In preparation for this lab, read Weiss' discussion of inner classes, sections 15.1-15.2, pages 574-580.

Programs for this lab may be found in this innerclasses directory.

Exercises

Exercise 1

Consider the following code:
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 Inner?
  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

package innerclasses;

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

Consider the Vector3d and its inner class:

package innerclasses;

import java.util.Iterator;
import java.util.NoSuchElementException;

/** Simple three-dimensional vector that is iterable over the dimensions. 
 *
 * @author Jerod Weinman
 */
public class Vector3D implements Iterable {

  private AnyType i,j,k; // Three components of the vector

  /** Construct a vector of the three specified components 
   *
   * @param i first component of the vector
   * @param j second component of the vector
   * @param k third component of the vector
   */
  public Vector3D(AnyType i, AnyType j, AnyType k) {
    this.i = i;
    this.j = j;
    this.k = k;
  }

  /**
   * Return an iterator over the components of this vector.
   *
   * @return iterator over the components of this vector
   */
  public Iterator iterator() { return new VectorIterator(); }

  /**
   * Iterator over the components of this vector.
   */
  private class VectorIterator implements Iterator {

    private int currentComponent = 0; 

    /** Determine whether there are more elements in this iterator.
     *
     * @return true if there are more elements 
     */
    public boolean hasNext() 
    { 
      return false; // YOUR CODE HERE
      
    }

    /** 
     * Return the next element of the vector.
     * 
     * @return the next element of the vector 
     * @throws NoSuchElementException when {@link hasNext} is false
     */
    public AnyType next() 
    { 
      return Vector3D.this.k; // Return an arbitrary component
    }

    /** 
     * Unsupported operation to remove an item from a vector of a
     * fixed-dimension. 
     *
     * @throws UnsupportedOperationException in any case
     */
    public void remove() { throw new UnsupportedOperationException (); }

  } /* VectorIterator */

  /** Driver program for the vector and its iterator */
  public static void main(String args[]) {

    // Create a vector of powers of two
    Vector3D vec = new Vector3D(4,8,16);

    // Get the iterator
    Iterator iter = vec.iterator();

    for (int component : vec) // Iterate and print the components
      System.out.println(component);
    
  }
}

  1. Upon inspection, do you expect the code to compile?
  2. Compile Vector3D.java. What happens? Try to come up with an explanation.
  3. Change line 36 to the following:
    private class VectorIterator<AnyType2> implements
    Iterator<AnyType2> {
    
  4. Now recompile the code. What happens? Why?
  5. Modify the return type of next() to resolve this problem.
  6. Recompile again. What is different from the first time you compiled (something subtle)? Does this make marginally more sense?
  7. Now change line 18 to the following:
    private class VectorIterator implements Iterator<AnyType> {
    
    and restore the return type of next() to AnyType.
  8. Recompile once again. Now what happens?
  9. 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.

Exercise5


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/207.sp012/labs/lab-inner-classes.shtml

created 12 January 2009 by Jerod Weinman
revsied 18 January 2011 by Jerod Weinman
revised 9-15 March 2012 by Henry M. Wallker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.


Copyright © 2011-2012 Henry M. Walker.
CC-BY-NC-SA
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .