Java Reference
In-Depth Information
27 data.add( new Rectangle( 20 , 30 , 5 , 15 ));
28
29 System.out.println( "Average area: " +
data.getAverage());
30 System.out.println( "Expected: 625" );
31
32 Rectangle max = (Rectangle)
data.getMaximum();
33 System.out.println( "Maximum area
rectangle: " + max);
34 System.out.println( "Expected:
java.awt.Rectangle[
35 x=10,y=20,width=30,height=40]" );
36 }
37 }
S ELF C HECK
11. Why would you use an inner class instead of a regular class?
12. How many class files are produced when you compile the
DataSetTester3 program?
A DVANCED T OPIC 9.2: Anonymous Classes
An entity is anonymous if it does not have a name. In a program, something that is
only used once doesn't usually need a name. For example, you can replace
Coin aCoin = new Coin(0.1, "dime");
data.add(aCoin);
with
data.add( new Coin(0.1, "dime") );
if the coin is not used elsewhere in the same method. The object new
Coin(0.1, "dime") is an anonymous object. Programmers like anonymous
objects, because they don't have to go through the trouble of coming up with a
name. If you have struggled with the decision whether to call a coin c , dime , or
aCoin , you'll understand this sentiment.
Search WWH ::




Custom Search