Mark Roth - Lab 4 Inner Class Lab - January 3, 1999

This lab is designed to give you some practice dealing with the four different types of inner classes. Don’t expect the example to make any intuitive sense. In a real-life scenario, you would not have class names like Outer, Inner and StaticMan. At the end of these 12 steps you should have a single file called Outer.java that compiles with no errors.  It is recommended that you print this out before attempting the lab.

  1. Type the following Code snippet into a file called Outer.java. Do not try to compile this yet.
public class Outer {

  private String inString = "Outer inString";
  private String outString = "Outer outString";

  public interface Printable {
    public void print();
  }

  public class Inner {
    private String inString = "Inner inString";
    public String getInString() { return this.inString; }
    public String getOutString() { return outString; }
  }

  public static class StaticMan {
    public void method1() {}
    void method2() {}
    private void method3() {}
    protected void method4() {}
    public String getOutString() {
      String result = "";
      result = outString;
      return result;
    }
  }
}

  1. Create a main method, and a constructor with no arguments, for the Outer class
  2. In the main method, create an instance of Outer called o, and a new instance of Inner called i.
  3. Still in the main method, invoke the getInString() and getOutString() methods on i. Write down what you expect to be printed for each:

  4. i.getInString(): ___________________

    i.getOutString(): ___________________
     

  5. Still in the main method, create an instance of StaticMan called sm.

  6. Invoke method1(), method2(), method3(), and method4() on sm. Which ones (if any) will not compile? If it will not compile, why not?

    method1(): __________________________________________________________

    method2(): __________________________________________________________

    method3(): __________________________________________________________

    method4(): __________________________________________________________
     

  7. The method getOutString() in the static inner class StaticMan will not compile. Why not?

  8. _____________________________________________________________________
     

  9. Change getOutString() in the static inner class StaticMan so that it will return "" instead of outString by commenting out (//) one of the 3 lines inside the method.
  10. In the constructor of Outer, create a new inner class called MInner, as follows:

  11. class MInner {
      public void method() {
        System.out.println( outString );
      }
    }
     

  12. Why did this inner class have to go in the constructor of Outer instead of inside the main method?

  13. _____________________________________________________________________

    _____________________________________________________________________
     

  14. In the constructor of Outer, create a new instance of MInner called m. Invoke method() on m. What do you expect to be printed?

  15. _____________________________________________________________________
     

  16. In the constructor of Outer, create an anonymous class that implements Printable (defined above). In it, override the print() method to print out your name. Create a new instance of this anonymous class called p, and invoke the print() method on p as follows:

  17.   p.print();
     

  18. Compile and run your program.