Java Reference
In-Depth Information
In this example, the ShowBits class is not known outside of main( ) , and any attempt to
access it by any method other than main( ) will result in an error.
One last point: You can create an inner class that does not have a name. This is called
an anonymous inner class . An object of an anonymous inner class is instantiated when the
class is declared, using new . Anonymous inner classes are discussed further in Chapter 16 .
Varargs: Variable-Length Arguments
Sometimes you will want to create a method that takes a variable number of arguments,
based on its precise usage. For example, a method that opens an Internet connection might
take a user name, password, file name, protocol, and so on, but supply defaults if some of
this information is not provided. In this situation, it would be convenient to pass only the
arguments to which the defaults did not apply. To create such a method implies that there
must be some way to create a list of arguments that is variable in length, rather than fixed.
In the past, methods that required a variable-length argument list could be handled two
ways, neither of which was particularly pleasing. First, if the maximum number of argu-
ments was small and known, then you could create overloaded versions of the method, one
for each way the method could be called. Although this works and is suitable for some situ-
ations, it applies to only a narrow class of situations. In cases where the maximum number
of potential arguments is larger, or unknowable, a second approach was used in which the
arguments were put into an array, and then the array was passed to the method. Frankly,
both of these approaches often resulted in clumsy solutions, and it was widely acknow-
ledged that a better approach was needed.
Ask the Expert
Search WWH ::




Custom Search