23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

is <strong>in</strong>tended to be used to <strong>in</strong>itialize the data associated with objects of this class so<br />

that they may be <strong>in</strong> a stable <strong>in</strong>itial state when first created.<br />

Constructors are <strong>in</strong>voked <strong>in</strong> a unique way: they must be called us<strong>in</strong>g the new<br />

operator. So, upon <strong>in</strong>vocation, a new <strong>in</strong>stance of this class is automatically created<br />

<strong>and</strong> its constructor is then called to <strong>in</strong>itialize its <strong>in</strong>stance variables <strong>and</strong> perform other<br />

setup tasks. For example, consider the follow<strong>in</strong>g constructor <strong>in</strong>vocation (which is<br />

also a declaration for the myFish variable):<br />

Fish myFish = new Fish (7, "Wally");<br />

A class can have many constructors, but each must have a different signature, that<br />

is, each must be dist<strong>in</strong>guished by the type <strong>and</strong> number of the parameters it takes.<br />

The ma<strong>in</strong> Method<br />

Some <strong>Java</strong> classes are meant to be used by other classes, others are meant to be<br />

st<strong>and</strong>-alone programs. Classes that def<strong>in</strong>e st<strong>and</strong>-alone programs must conta<strong>in</strong> one<br />

other special k<strong>in</strong>d of method for a class—the ma<strong>in</strong> method. When we wish to<br />

execute a st<strong>and</strong>-alone <strong>Java</strong> program, we reference the name of the class that def<strong>in</strong>es<br />

this program by issu<strong>in</strong>g the follow<strong>in</strong>g comm<strong>and</strong> (<strong>in</strong> a W<strong>in</strong>dows, L<strong>in</strong>ux, or UNIX<br />

shell):<br />

java Aquarium<br />

In this case, the <strong>Java</strong> run-time system looks for a compiled version of the<br />

Aquarium class, <strong>and</strong> then <strong>in</strong>vokes the special ma<strong>in</strong> method <strong>in</strong> that class. This<br />

method must be declared as follows:<br />

public static voidma<strong>in</strong>(Str<strong>in</strong>g[] args){<br />

}<br />

// ma<strong>in</strong> method body …<br />

The arguments passed as the parameter args to the ma<strong>in</strong> method are the<br />

comm<strong>and</strong>l<strong>in</strong>e arguments given when the program is called. The args variable is an<br />

array of Str<strong>in</strong>g objects, that is, a collection of <strong>in</strong>dexed str<strong>in</strong>gs, with the first str<strong>in</strong>g<br />

be<strong>in</strong>g args[0], the second be<strong>in</strong>g args[1], <strong>and</strong> so on. (We say more about arrays <strong>in</strong><br />

Section 1.5.)<br />

Call<strong>in</strong>g a <strong>Java</strong> Program from the Comm<strong>and</strong> L<strong>in</strong>e<br />

<strong>Java</strong> programs can be called from the comm<strong>and</strong> l<strong>in</strong>e us<strong>in</strong>g the java comm<strong>and</strong>,<br />

followed by the name of the <strong>Java</strong> class whose ma<strong>in</strong> method we wish to run, plus<br />

39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!