メインコンテンツにスキップ

Java keywords

In Java, keywords are reserved words that have a predefined meaning in the language. They form the foundation of Java's syntax and cannot be used as identifiers, such as variable names, method names, or class names.

Each keyword has a specific function, and the Java compiler uses these to parse the structure of Java code.

List of Java Keywords

KeywordDescription
abstractDefines abstract classes and methods.
assertUsed for debugging purposes to test assumptions in the code.
booleanDefines a boolean variable with true or false values.
breakExits a loop or a switch statement.
byteDefines a byte variable (8-bit signed integer).
caseDefines a block of code in switch statements.
catchHandles exceptions in a try-catch block.
charDefines a character variable.
classDeclares a class.
continueSkips the current iteration of a loop and continues.
defaultDefines the default block in a switch statement.
doExecutes a block of code at least once in a do-while loop.
doubleDefines a double precision floating-point variable.
elseDefines the alternative block for an if statement.
enumDeclares an enumerated type.
extendsIndicates a subclass inherits from a superclass.
finalDefines constants or methods that cannot be overridden.
finallyEnsures code execution after a try-catch block.
floatDefines a floating-point variable.
forDeclares a loop that repeats a block of code.
ifStarts a conditional block.
implementsIndicates that a class implements an interface.
importImports classes or entire packages.
instanceofTests whether an object is an instance of a specific class.
intDefines an integer variable.
interfaceDeclares an interface.
longDefines a long integer variable.
nativeIndicates that a method is implemented in native code.
newCreates a new object instance.
nullRepresents the null reference, no value.
packageDeclares a package.
privateDeclares a private member, accessible only within its class.
protectedDeclares a protected member, accessible within package or subclasses.
publicDeclares a public member, accessible by any other class.
returnExits a method and optionally returns a value.
shortDefines a short integer variable.
staticDeclares a static member that belongs to the class, not instances.
strictfpRestricts floating-point calculations to ensure portability.
superRefers to the superclass of the current object.
switchDeclares a multi-branch conditional statement.
synchronizedLocks a block of code or method to prevent concurrent access.
thisRefers to the current object instance.
throwThrows an exception.
throwsDeclares exceptions that a method can throw.
transientMarks a field to be skipped during serialization.
tryStarts a block to test for exceptions.
voidIndicates that a method does not return a value.
volatileMarks a variable as being used by multiple threads.
whileDeclares a loop that executes while a condition is true.

Tips and Best Practices

  • Avoid using Java keywords as variable names: Trying to use a keyword as a variable or identifier will result in a syntax error.
  • Use meaningful names: When naming classes, methods, or variables, ensure they are descriptive and avoid abbreviations that are unclear.
  • Keep keywords in mind when working with frameworks: Be mindful of keywords when you generate classes or methods dynamically, especially when using frameworks that auto-generate code.
  • Organize code using keywords effectively: Make good use of keywords like final, synchronized, and volatile in multithreaded applications for thread safety and performance.