Java Reference
In-Depth Information
Note Javadoesnotcreateadefaultnoargumentconstructorwhenatleastonecon-
structor is declared.
Specifying Constructor Parameters and Local Variables
Youexplicitlydeclareaconstructorwithinaclass'sbodybyspecifyingthenameofthe
classfollowedbya parameter list ,whichisaroundbracket-delimitedandcomma-sep-
aratedlistofzeroormoreparameterdeclarations.A parameter isaconstructorormeth-
odvariablethatreceivesanexpressionvaluepassedtotheconstructorormethodwhen
it is called. This expression value is known as an argument .
Listing2-2 enhances Listing2-1 's Image classbydeclaringthreeconstructorswith
parameterliststhatdeclarezero,one,ortwoparameters;anda main() methodfortest-
ing this class.
Listing 2-2. Declaring an Image class with three constructors and a main() method
class Image
{
Image()
{
System.out.println("Image() called");
}
Image(String filename)
{
this(filename, null);
System.out.println("Image(String filename) called");
}
Image(String filename, String imageType)
{
System.out.println("Image(String
filename,
String
imageType) called");
if (filename != null)
{
System.out.println("reading "+filename);
if (imageType != null)
System.out.println("interpreting
"+filename+"
as storing a "+
 
Search WWH ::




Custom Search