Keyword In Java Program



A predefined identifier that has special meaning in a Java program outside comment and string is called a Keyword. Or, A keyword is a reserved word in Java language inside the compiler and JVM that perform a unique and special operation. In the Java programming language, the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type. This means that only one instance of that static member is created which is shared across all instances of the class. This keyword in Java is used to refer to the current object. It can be used for different purposes like Variable or Method call of the current Object or can be passed as a method parameter. Variable Hiding is also possible using this keyword. The Java programming language has 50 keywords. Each keyword has a specific meaning in the language. You can’t use a keyword for anything other than its pre-assigned meaning. The following table lists Java’s keywords.

Advertisements

Java keywords are also known as reserved words. Keywords are particular words which acts as a key to a code. These are predefined words by Java so it cannot be used as a variable or object name. List of Java Keywords. A list of Java keywords or reserved words are given below: abstract: Java abstract keyword is used to declare.

Java Throw Keyword

Java throw keyword is used to throw an exception explicitly. The throw keyword must be in try block. When JVM encounters the throw keyword, it stops the execution of try block and jump to the corresponding catch block.

Java

Example of throw keyword in java

In the above example, statement 1 is validating the value of b, if b is zero then, throw an exception otherwise show the result.

Every time, when an exception occur, try block throws an exception to corresponding catch block. When we need a method to throw an exception, we use throws keyword followed by method declaration.

When a method throws an exception, we must put the calling statement of method in try-catch block.

Example of throws keyword in java

In the above example, division() method is throwing an ArithmeticException, therefore, we have written the calling statement of division() method in try-catch block.

Java Finally Keyword

Finally block is used to cleanup resources allocated by try block, such as closing file, closing database connection, etc. The catch block is executed when the exception occur in try block. The finally block is used to execute a given set of statements, whether an exception has occurred or not. We can have only one finally block for each try block. It is not mandatory to have a finally block after try-catch block.

Example of finally keyword in java

Advertisements

Java Throw Keyword

Java throw keyword is used to throw an exception explicitly. The throw keyword must be in try block. When JVM encounters the throw keyword, it stops the execution of try block and jump to the corresponding catch block.

Example of throw keyword in java

In the above example, statement 1 is validating the value of b, if b is zero then, throw an exception otherwise show the result.

Every time, when an exception occur, try block throws an exception to corresponding catch block. When we need a method to throw an exception, we use throws keyword followed by method declaration.

When a method throws an exception, we must put the calling statement of method in try-catch block.

Super Keyword In Java Program

Example of throws keyword in java

In the above example, division() method is throwing an ArithmeticException, therefore, we have written the calling statement of division() method in try-catch block.

Java Finally Keyword

Finally block is used to cleanup resources allocated by try block, such as closing file, closing database connection, etc. The catch block is executed when the exception occur in try block. The finally block is used to execute a given set of statements, whether an exception has occurred or not. We can have only one finally block for each try block. It is not mandatory to have a finally block after try-catch block.

Final Keyword In Java Program

Example of finally keyword in java