Sunday, August 15, 2010

Inner Classes: Pastry.java

Courtesy of Sun Web Learning Center

1. class Pastry {
2.   public static class Filling {
3.     public void berry() { System.out.println("yum "); }
4.   }
5. }
6. class Bakery {
7.   public static void main(String [] args) {
8.     // insert code here
9.     f.berry();
10.   }
11. }

Which, inserted at line 8, creates the output yum ?

A     Filling f = new Filling();
B     Pastry.Filling f = new Pastry.Filling();
C     Pastry p = new Pastry(); Filling f = new p.Filling();
D     Pastry p = new Pastry(); Pastry.Filling f = new p.Filling();

B is correct. This is the correct syntax to instantiate this inner class.

No comments:

Post a Comment