Java Reference
In-Depth Information
You specify a single static member name to import only that name:
import static java.lang.Math.PI; // Import the PI static
field only.
import static java.lang.Math.cos; // Import the cos() stat-
ic method only.
In contrast, you specify the wildcard to import all static member names:
import static java.lang.Math.*;
// Import all static mem-
bers from Math.
You can ow refer to the static member(s) without having to specify the class name:
System.out.println(cos(PI));
Usingmultiplestaticimportstatementscanresultinnameconflicts,whichcausesthe
compilertoreporterrors.Forexample,supposeyour geom packagecontainsa Circle
class with a static member named PI . ow suppose you specify import static
java.lang.Math.*; and import static geom.Circle.*; at the top of
yoursourcefile.Finally,supposeyouspecify System.out.println(PI); some-
whereinthatfile'scode.Thecompilerreportsanerrorbecauseitdoesnotknowif PI
belongs to Math or Circle .
Exceptions
In an ideal world, othing bad ever happens when an application runs. For example, a
filealwaysexistswhentheapplicationneedstoopenthefile,theapplicationisalways
abletoconnecttoaremotecomputer,andtheJVMeverrunsoutofmemorywhenthe
application needs to instantiate objects.
Incontrast,real-worldapplicationsoccasionallyattempttoopenfilesthatdonotex-
ist,attempttoconnecttoremotecomputersthatareunabletocommunicatewiththem,
and require more memory than the JVM can provide. Your goal is to write code that
properly responds to these and other exceptional situations (exceptions).
Thissectionintroducesyoutoexceptions.Afterdefiningthisterm,thesectionlooks
at representing exceptions in source code. It then examines the topics of throwing and
handlingexceptions,andconcludesbydiscussinghowtoperformcleanuptasksbefore
a method returns, whether or not an exception has been thrown.
Search WWH ::




Custom Search