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.

a text editor class may wish to def<strong>in</strong>e a related cursor class. Def<strong>in</strong><strong>in</strong>g the cursor<br />

class as a nested class <strong>in</strong>side the def<strong>in</strong>ition of the text editor class keeps these two<br />

highly related classes together <strong>in</strong> the same file. Moreover, it also allows each of<br />

them to access nonpublic methods of the other. One technical po<strong>in</strong>t regard<strong>in</strong>g<br />

nested classes is that the nested class should be declared as static. This<br />

declaration implies that the nested class is associated with the outer class, not an<br />

<strong>in</strong>stance of the outer class, that is, a specific object.<br />

Packages<br />

A set of classes, all def<strong>in</strong>ed <strong>in</strong> a common subdirectory, can be a <strong>Java</strong> package.<br />

Every file <strong>in</strong> a package starts with the l<strong>in</strong>e:<br />

package package_name;<br />

The subdirectory conta<strong>in</strong><strong>in</strong>g the package must be named the same as the package.<br />

We can also def<strong>in</strong>e a package <strong>in</strong> a s<strong>in</strong>gle file that conta<strong>in</strong>s several class def<strong>in</strong>itions,<br />

but when it is compiled, all the classes will be compiled <strong>in</strong>to separate files <strong>in</strong> the<br />

same subdirectory.<br />

In <strong>Java</strong>, we can use classes that are def<strong>in</strong>ed <strong>in</strong> other packages by prefix<strong>in</strong>g class<br />

names with dots (that is, us<strong>in</strong>g the '.' character) that correspond to the other<br />

packages' directory structures.<br />

public boolean Temperature(TA.Measures.Thermometer<br />

thermometer,<br />

//…<br />

}<br />

<strong>in</strong>t temperature) {<br />

The function Temperature takes a class Thermometer as a parameter.<br />

Thermometer is def<strong>in</strong>ed <strong>in</strong> the TA package <strong>in</strong> a subpackage called Measures.<br />

The dots <strong>in</strong> TA.Measures.Thermometer correspond directly to the directory<br />

structure <strong>in</strong> the TA package.<br />

All the extra typ<strong>in</strong>g needed to refer to a class outside of the current package can get<br />

tir<strong>in</strong>g. In <strong>Java</strong>, we can use the import keyword to <strong>in</strong>clude external classes or<br />

entire packages <strong>in</strong> the current file. To import an <strong>in</strong>dividual class from a specific<br />

package, we type the follow<strong>in</strong>g at the beg<strong>in</strong>n<strong>in</strong>g of the file:<br />

import packageName.classNames;<br />

For example, we could type<br />

73

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

Saved successfully!

Ooh no, something went wrong!