Java Reference
In-Depth Information
class Image
// various member declarations
Listing2-1 declaresaclassnamed Image ,whichpresumablydescribessomekindof
image for displaying on the screen. By convention, a class's name begins with an up-
name is capitalized. This is known as camelcasing .
Creating Objects with the new Operator and a Constructor
Image is an example of a user-defined type from which objects can be created. You
create these objects by using the new operator with a constructor, as follows:
Image image = new Image();
The new operator allocates memory to store the object whose type is specified by
new 'ssolitary operand, which happens to be Image() in this example. The object is
stored in a region of memory known as the heap .
Theparentheses(roundbrackets)thatfollow Image signifya constructor ,whichis
a block of code for constructing an object by initializing it in some manner. The new
operator invokes (calls)theconstructorimmediatelyafterallocatingmemorytostorethe
Whentheconstructorends, new returnsa reference (amemoryaddressorotheriden-
tifier) totheobject sothat itcanbeaccessed elsewhere intheprogram.Regarding the
newlycreated Image object,itsreferenceisstoredinavariablenamed image whose
typeisspecifiedas Image .(It'scommontorefertothevariableasanobject,asinthe
image object, although it stores only an object's reference and not the object itself.)
Note new 's returned reference is represented in source code by keyword this .
Wherever this appears,itrepresentsthecurrentobject.Also,variablesthatstoreref-
erences are called reference variables .
Image doesnotexplicitlydeclareaconstructor.Whenaclassdoesnotdeclareacon-
structor, Java implicitly creates a constructor for that class. The created constructor is
knownasthe default noargument constructor becausenoarguments(discussedshortly)
appear between its ( and ) characters when the constructor is invoked.
Search WWH ::

Custom Search