Java Reference
In-Depth Information
You can define a class inside a method — where it is called a local nested class . It is also referred to as
a local inner class because a non-static nested class is often referred to as an inner class . You can create
objects of a local inner class only locally — that is, within the method in which the class definition appears.
This is useful when the computation in a method requires the use of a specialized class that is not required
or used elsewhere. A good example is listeners for events that arise as a result of user interaction with an
application. You learn about listeners in Chapter 18.
A local inner class can refer to variables declared in the method in which the definition appears, but only
if they are final .
SUMMARY
In this chapter you learned the essentials of defining your own classes. You can now create your own class
types to fit the context of the problems you are dealing with. You will build on this in the next chapter to
enable you to add more flexibility to the operations on your class objects by learning how to realize inherit-
ance.
EXERCISES
You can download the source code for the examples in the topic and the solutions to the following exer-
cises from www.wrox.com .
1. Define a class for rectangle objects defined by two points: the top-left and bottom-right corners of the
rectangle. Include a constructor to copy a rectangle, a method to return a rectangle object that encloses
the current object and the rectangle passed as an argument, and a method to display the defining points of
a rectangle. Test the class by creating four rectangles and combining these cumulatively to end up with a
rectangle enclosing them all. Output the defining points of all the rectangles you create.
2. Define a class, mcmLength , to represent a length measured in meters, centimeters, and millimeters, each
stored as integers. Include methods to add and subtract objects, to multiply and divide an object by an in-
teger value, to calculate an area resulting from the product of two objects, and to compare objects. Include
constructors that accept three arguments — meters, centimeters, and millimeters; one integer argument
in millimeters; one double argument in centimeters; and no arguments, which creates an object with the
length set to zero. Check the class by creating some objects and testing the class operations.
3. Define a class, tkgWeight , to represent a weight in tons, kilograms, and grams, and include a similar
range of methods and constructors as the previous example. Demonstrate this class by creating and com-
bining some class objects.
4. Put both the previous classes in a package called Measures . Import this package into a program that
calculates and displays the total weight of the following: 200 carpets — size: 4 meters by 2 meters 9 cen-
timeters, that weigh 1.25 kilograms per square meter; and 60 carpets — size: 3 meters 57 centimeters by
5 meters, that weigh 1.05 kilograms per square meter.
• WHAT YOU LEARNED IN THIS CHAPTER
Search WWH ::




Custom Search