Java Reference
In-Depth Information
imageType+" image");
}
// Perform other initialization here.
}
public static void main(String[] args)
{
Image image = new Image();
System.out.println();
image = new Image("image.png");
System.out.println();
image = new Image("image.png", "PNG");
}
}
Image
objecttodefaultvalues(whatevertheymaybe).Thisconstructorsimulatesde-
faultinitializationbyinvoking
System.out.println()
tooutputamessagesigni-
fying that it's been called.
Image
next declares an
Image(String filename)
constructor whose para-
meter list consists of a single parameter declaration—a parameter declaration consists
ofavariable'stypefollowedbythevariable'sname.The
java.lang.String
para-
meterisnamed
filename
,signifyingthatthisconstructorobtainsimagecontentfrom
a file.
Note
Throughoutthisbook'schapters,Itypicallyprefixthefirstuseofapredefined
type(suchas
String
)withthepackagehierarchyinwhichthetypeisstored.Forex-
ample,
String
isstoredinthe
lang
subpackageofthe
java
package.Idosotohelp
youlearnwheretypesarestoredsothatyoucanmoreeasilyspecifyimportstatements
forimportingthesetypes(withouthavingtofirstsearchforatype'spackage)intoyour
sourcecode—youdon'thavetoimporttypesthatarestoredinthe
java.lang
pack-
age, but I still prefix the
java.lang
package to the type name for completeness. I
will have more to say about packages and the import statement in
Chapter 3
.
Someconstructorsrelyonotherconstructorstohelptheminitializetheirobjects.This
isdonetoavoidredundantcode,whichincreasesthesizeofanobject,andunnecessarily
takesmemoryawayfromtheheapthatcouldbeusedforotherpurposes.Forexample,
Image(String filename)
relies on
Image(String filename, String
imageType)
to read the file's image content into memory.