Java Reference
In-Depth Information
lect.nextInt(maxRabbits)
. Calling
nextInt()
with the argument
maxRabbits
returns a value that is
from
0
to
maxRabbits-1
, inclusive. Adding
1
to this results in a value from 1 to
maxRabbits
, inclusive.
The array so created is then filled with
Rabbit
objects.
The
MagicHat
class also has a
toString()
method that returns a
String
object containing the name
of the hat and the names of all the rabbits in the hat. This assumes that the
Rabbit
class also has a
toString()
method defined. You are able to use the
toString()
implicitly in an output statement when
you create and display
MagicHat
class objects.
The base names that you use to generate rabbit names are defined in the
static
array
rabbitNames[]
in the
Rabbit
class. The count for each base name, which you append to the base name to produce a
unique name for a rabbit, is stored in the
static
array
rabbitNamesCount[]
. This has the same number
of elements as the
rabbitNames
array, and each element stores a value to be appended to the correspond-
ing name in the
rabbitNames
array. The
Rabbit
class has the data member
name
to store a name that is
initialized in the constructor. A random base name is selected from the
rabbitNames[]
array using an
index value from 0 up to one less than the length of this array. You then append the current count for the
name incremented by 1, so successive uses of any base name, such as
Gnasher
, for example, produce
names
Gnasher1
,
Gnasher2,
and so on. The
toString()
method for the class returns the name for the
Rabbit
object.
The
main()
method in
TryNestedClass
creates three
MagicHat
objects and outputs the string represent-
ation of each of them. Putting the object as an argument to the
println()
method calls the
toString()
method for the object automatically, and the
String
object that is returned is output to the screen.
If you look at the
.class
files that are produced by the compiler, the
Rabbit
class has its own file with
the name
MagicHat*Rabbit.class
. Thus the name of the nested
Rabbit
class is qualified with the name
of the class that contains it,
MagicHat
.
Using a Non-Static Nested Class
In the previous example, you could make the
Rabbit
class non-static by deleting the keyword
static
from
its definition. However, if you try that, the program no longer compiles and runs. The problem is the static
data members
rabbitNames
and
rabbitNamesCount
in the
Rabbit
class. You saw earlier that a non-static
nested class cannot have static members, so you must find an alternative way of dealing with names if you
want to make
Rabbit
a non-static nested class.
You could consider making these arrays non-static. This has several disadvantages. First, each
Rabbit
object would have its own copy of these arrays — an unnecessary duplication of data. A more serious
problem is that the naming process would not work. Because each object has its own copy of the
rab-
bitNamesCount
array, the names that are generated are not going to be unique.
The answer is to keep
rabbitNames
and
rabbitNamesCount
as static, but put them in the
MagicHat
class
instead. Let's see that working.
TRY IT OUT: Accessing the Top-Level Class Members
You need to modify the class definition to the following: