Java Reference
In-Depth Information
Classes that are declared outside of any class are known as top-level classes. Java
alsosupportsnestedclasses,whichareclassesdeclaredasmembersofotherclassesor
scopes, and which help you implement top-level class architecture.
There are four kinds of nested classes: static member classes, nonstatic member
classes,anonymousclasses,andlocalclasses.Thelatterthreecategoriesareknownas
inner classes.
Java supports the partitioning of top-level types into multiple namespaces, to better
organizethesetypesandtoalsopreventnameconflicts.Javausespackagestoaccom-
plish these tasks.
The package statement identifies the package in which a source file's types are loc-
ated.Theimportstatementimportstypesfromapackagebytellingthecompilerwhere
to look for unqualified type names during compilation.
Anexceptionisadivergencefromanapplication'snormalbehavior.Althoughitcan
be represented by an error code or object, Java uses objects because error codes are
meaningless and cannot contain information about what led to the exception.
Javaprovidesahierarchyofclassesthatrepresentdifferentkindsofexceptions.These
classes are rooted in Throwable . Moving down the throwable hierarchy, you en-
counterthe Exception and Error classes,whichrepresentnonerrorexceptionsand
errors.
Exception and its subclasses, except for RuntimeException (and its sub-
classes), describe checked exceptions. They are checked because the compiler checks
the code to ensure that an exception is handled where thrown or identified as being
handled elsewhere.
RuntimeException anditssubclassesdescribeuncheckedexceptions.Youdonot
have to handle these exceptions because they represent coding mistakes (fix the mis-
takes).Althoughthenamesoftheirclassescanappearinthrowsclauses,doingsoadds
clutter.
The throw statement throws an exception to the JVM, which searches for an appro-
priatehandler.Iftheexceptionischecked,itsnamemustappearinthemethod'sthrows
clause, unless the name of the exception's superclass is listed in this clause.
A method handles one or more exceptions by specifying a try statement and appro-
priatecatchblocks.Afinallyblockcanbeincludedtoexecutecleanupcodewhetheror
not an exception is thrown, and before a thrown exception leaves the method.
Anassertionisastatementthatletsyouexpressanassumptionofapplicationcorrect-
nessviaaBooleanexpression.Ifthisexpressionevaluatestotrue,executioncontinues
with the ext statement; otherwise, an error that identifies the cause of failure is thrown.
Search WWH ::




Custom Search