Table of content

Keywords

Keywords are reserved identifiers that are predefined in the language and cannot be used to denote other entities. All the keywords are in lowercase, and incorrect usage results in compilation errors.

Keywords currently defined in the language are listed below in the table. In addition, three identifiers are reserved as predefined literals in the language: the null reference and the Boolean literals true and false.

Keywords currently reserved, but not in use, are listed in the below table; these reserved words cannot be used as identifiers. The index contains references to relevant sections where currently defined keywords are explained.

keywords-java

Examples of using a keyword in java

abstract

The abstract keyword can be used in two places in java. You can use it to defining a class to make it abstract.

public abstract class Helloworld

You can use it to defining a method to make it abstract.

public abstract void doStuff();
implements

The implements keyword included in the class declaration to specify any interfaces that are implemented by the current class.

public abstract class Helloworld implements IHelloworld
package

A group of types. Packages are declared with the package keyword; this keyword is used for the naming convention of java files.

package com.java.connect;
import

The import keyword used at the beginning of a source file that can specify classes or entire packages to be referred to later without including their package names in the reference.

import java.util.*;
public

The public keyword used in a method or variable declaration and class level. It signifies that the method or variable or class can be accessed by elements residing in other classes.

public class Helloworld
protected

The protected keyword used in a method or variable declaration. It means that the method or variable can only be accessed by elements residing in its class, subclasses, or classes in the same package.

protected void doStuff()
private

The private keyword used in a method or variable declaration. It means that the method or variable can only be accessed by other elements of its class.

private void doStuff()
class

The class keyword in the Java programming language, a type that defines the implementation of a particular kind of object.

A class definition defines instance and class variables and methods, as well as specifying the interfaces the class implements and the immediate superclass of the class. If the superclass is not explicitly specified, the superclass will implicitly be Object.

class Helloworld
interface

The interface keyword used to define a collection of method definitions and constant values. It can later be implemented by classes that define this interface with the “implements�? keyword.

public interface IHelloworld
extends

Class X extends class Y to add functionality, either by adding fields or methods to class Y or by overriding methods of class Y. An interface extends another interface by adding methods. Class X is said to be a subclass of class Y.

public class Helloworld extends SuperHelloworld
implements

The implements keyword included in the class declaration to specify any interfaces that are implemented by the current class.

public class Helloworld implements IHelloworld
this

The this keyword can be used to represent an instance of the class in which it appears. this can be used to access class variables and methods.

this.doStuff();
super

The super keyword used to access members of a class inherited by the class in which it appears.

super.doStuff();
null

The null keyword is a type that has one value, the null reference, represented by the literal null, which is formed from ASCII characters. A null literal is always of the null type.

private String name = null;
void

The void keyword used in method declarations to specify that the method does not return any value. The void can also be used as a nonfunctional statement.

protected void doStuff()
return

The return keyword used to finish the execution of a method. It can be followed by a value required by the method definition.

public int add(){	return 5 + 4;}
static

The static keyword used to define a variable as a class variable. Classes maintain one copy of class variables regardless of how many instances exist of that class. static can also be used to define a method as a class method.

Class methods are invoked by the class instead of a specific instance, and can only operate on class variables. The static also can be used to define for static initiation block.

private static int age = 25;

static{
    // do some stuff which will be loaded when class loaded in memory
}

public static int add(){
    return 5 + 4;
}
new

The new keyword used to create an instance of a class.

Helloworld obj = new Helloworld();
boolean

The boolean keyword refers to an expression or variable that can have only a true or false value. The Java programming language provides the boolean type and the literal values true and false.

boolean flag = true
byte

The byte keyword represents a sequence of eight bits. Java provides a corresponding byte type.

byte no = 3;
char

The char keyword used to declare a variable of type character.

char x = 'x';
double

The double keyword used to define a variable of type double.

double price = 5.67;
float

The float keyword used to define a floating-point number variable.

float price = 5;
int

The int keyword used to define a variable of type integer.

int price = 5;
long

The long keyword used to define a variable of type long.

long price = 5;
short

The short keyword used to define a variable of type short.

short price = 5;
switch

The switch keyword used to evaluate a variable that can later be matched with a value specified by the case keyword in order to execute a group of statements.

case

The case keyword that defines a group of statements to begin executing if a value specified matches the value defined by a preceding switch keyword.

break

The break keyword used to resume program execution at the statement immediately following the current statement. If followed by a label, the program resumes execution at the labeled statement.

default

The default keyword optionally used after all case conditions in a switch statement. If the value of the switch variable does not match all case conditions, the default keyword will be executed.

switch (key) {
    case value:
        break;
    default:
        break;
}
continue

The continue keyword used to resume program execution at the end of the current loop. If followed by a label, continue resumes execution where the label occurs.

public void doStuff() {
    for (int i = 1; i < 10; i++) {
        if (i == 2) {
            continue;
            // The control will go back and never reach this line.
            // a = i;
        }
    }
}
do

The do keyword used to declare a loop that will iterate a block of statements. The loop’s exit condition can be specified with the while keyword.

do {
        // do stuffs
    } while (condition);
while

The while keyword used to declare a loop that iterates a block of statements. The loop’s exit condition is specified as part of the while statement.

while (condition) {
        // do stuffs
        }
try

A Java keyword that defines a block of statements that may throw a Java language exception. If an exception is thrown, an optional catch block can handle specific exceptions thrown within the try block.

Also, an optional finally block will be executed regardless of whether an exception is thrown or not.

try {
        // Do some stuff
    } catch (Exception e) {
        // handle exception
    }
catch

A Java keyword used to declare a block of statements to be executed in the event that a Java exception, or run time error, occurs in a preceding try block.

try {
        // Do some stuff
    } catch (Exception e) {
        // handle exception
    }
throw

A throw keyword used to define the user-defined exceptions that allow the user to throw an exception or any class that implements the throwable interface.

 if(condition){
        // Do some stuff
    }else{
        // else throw some exception
        throw new Exception();
    }
throws

A throws keyword used in method declarations that specify which exceptions are not handled within the method but rather passed to the next higher level of the program.

public void doStuff() throws Exception{
        // Do some stuff
    }
final

The final keyword can be used at variable, class, and method level if you define an entity once as final and cannot change it or derive from it later.

More specifically: a final class cannot be subclassed, a final method cannot be overridden, and a final variable cannot change from its initialized value.

final String name = "RAM";

    public final class HelloWorld

    public final void doStuff()
finally

The finally keyword will always come with a try block that executes a block of statements regardless of whether a Java Exception or run time error, occurred in a block defined previously by the try keyword, The finally block will always execute other than System.exit().

try {
        // Do some stuff
    } catch (Exception e) {
        // handle exception
    }finally{
        // So the stuff that has to be executed at any conditions
    }
if

The if keyword used to conduct a conditional test and execute a block of statements if the test evaluates to true.

 if (condition) {
        // Do some stuff
    }
else

The else keyword used to execute a block of statements in the case that the test condition with the if keyword evaluates to false.

 if (condition) {
        // Do some stuff
    }else{
        // Do other stuff
    }
for

The for keyword used to declare a loop that reiterates statements. The programmer can specify the statements to be executed, exit conditions, and initialization variables for the loop.

for (int i = 0; i < array.length; i++) {
        // do some stuff
    }
enum

The keyword enum used to declare an enumerated type.

public enum Helloworld
synchronized

The synchronized keyword in the Java programming language that, when applied to a method or code block, guarantees that at most one thread at a time executes that code.

 synchronized (value) {
        // do some stuff
    }
instanceof

The instanceof keyword used for tests two-argument whether the runtime type of its first argument is assignment compatible with its second argument.

Integer value;
    protected void doStuff() {
        if(value instanceof Integer){
            // do some stuff
        }
    }
transient

The transient keyword used in the Java programming language that indicates that a field is not part of the serialized form of an object. When an object is serialized, the values of its transient fields are not included in the serial representation, while the values of its non-transient fields are included.

transient int value;
volatile

The volatile keyword used in variable declarations that specifies that the variable is modified asynchronously by concurrently running threads.

volatile int value;
native

The native keyword that is used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language.

const

The const is a reserved Java keyword not used by current versions of the Java programming language.

goto

The goto is a reserved Java keyword. However, it is not used by current versions of the Java programming language.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions