Keywords

Keywords are identifiers like public, static and class that have a special meaning inside Java source code and outside of comments and Strings. Four keywords are used in Hello World, public, static, void and class.

Keywords are reserved for their intended use and cannot be used by the programmer for variable or method names.

There are fifty reserved keywords in Java 1.1 (51 in Java 1.2). The forty-eight that are actually used in are listed below. Don't worry if the purposes of the keywords seem a little opaque at this point. They will all be explained in much greater detail later.

Keywords Used in Java 1.1

KeywordPurpose
abstractdeclares that a class or method is abstract
booleandeclares a boolean variable or return type
breakprematurely exits a loop
bytedeclares a byte variable or return type
caseone case in a switch statement
catchhandle an exception
chardeclares a character variable or return type
classsignals the beginning of a class definition
continueprematurely return to the beginning of a loop
defaultdefault action for a switch statement
dobegins a do while loop
doubledeclares a double variable or return type
elsesignals the code to be executed if an if statement is not true
extendsspecifies the class which this class is a subclass of
finaldeclares that a class may not be subclassed or that a field or method may not be overridden
finallydeclares a block of code guaranteed to be executed
floatdeclares a floating point variable or return type
forbegins a for loop
ifexecute statements if the condition is true
implementsdeclares that this class implements the given interface
importpermit access to a class or group of classes in a package
instanceoftests whether an object is an instanceof a class
intdeclares an integer variable or return type
interfacesignals the beginning of an interface definition
longdeclares a long integer variable or return type
nativedeclares that a method is implemented in native code
newallocates a new object
packagedefines the package in which this source code file belongs
privatedeclares a method or member variable to be private
protecteddeclares a class, method or member variable to be protected
publicdeclares a class, method or member variable to be public
returnreturns a value from a method
shortdeclares a short integer variable or return type
staticdeclares that a field or a method belongs to a class rather than an object
supera reference to the parent of the current object
switchtests for the truth of various possible cases
synchronizedIndicates that a section of code is not thread-safe
thisa reference to the current object
throwthrow an exception
throwsdeclares the exceptions thrown by a method
transientThis field should not be serialized
tryattempt an operation that may throw an exception
voiddeclare that a method does not return a value
volatileWarns the compiler that a variable changes asynchronously
whilebegins a while loop

Two other keywords, const and goto, are reserved by Java but are not actually implemented. This allows compilers to produce better error messages if these common C++ keywords are improperly used in a Java program.

Java 1.2 adds the strictfp keyword to declare that a method or class must be run with exact IEEE 754 semantics.

true and false appear to be missing from this list. In fact, they are not keywords but rather boolean literals. You still can't use them as a variable name though.


Previous | Next | Top
Last Modified March 9, 2000
Copyright 1997, 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu