Keywords Java
Keywords Java
Final Variable: When a variable is declared as final, its value cannot be changed once
assigned. This makes it a constant.
The finally block is used in conjunction with try and catch to ensure that certain code is executed
after a try-catch block, regardless of whether an exception was thrown or not. It is typically used to
release resources like file streams or database connections .Even if an exception is thrown or the
program is terminated in the try block, the finally block will execute.
The super keyword is used to refer to the superclass (parent class) of the current object. It is used
for:
The this keyword is used to refer to the current instance of the class. It is often used to:
Differentiate between instance variables and parameters with the same name.
The abstract keyword is used to define abstract classes and methods. An abstract class cannot be
instantiated, and an abstract method must be implemented by a subclass.
Abstract Class: A class that cannot be instantiated but may have abstract methods.
Abstract Method: A method in an abstract class that does not have a body, only a
declaration..
The extends keyword is used to indicate that a class is inheriting from a superclass. It establishes
an is-a relationship.
The implements keyword is used when a class wants to implement an interface. A class must provide
concrete implementations for all methods declared in the interface.
The throw keyword is used to explicitly throw an exception from within a method or block of code. It
can throw both checked and unchecked exceptions.
In this example, the throw keyword is used to throw an IllegalArgumentException if the age is less
than 18.
9. throws (Used in method declaration)
The throws keyword is used in a method declaration to specify that the method may throw certain
exceptions. It informs the caller of the method that they need to handle or declare the exceptions.
The synchronized keyword is used to control access to a method or block of code by multiple
threads. When a method or block is synchronized, only one thread can execute it at a time.
Synchronized Method:
count++;
Synchronized Block:
void someMethod() {
synchronized(this) {
The synchronized keyword helps prevent race conditions when multiple threads access shared
resources.
The interface keyword is used to declare an interface. An interface defines a contract that classes can
implement. Interfaces cannot have method bodies (except for default or static methods introduced
in Java 8).
The enum keyword is used to define an enum type, which is a special class representing a fixed set of
constants.
13. try (Used for exception handling)
The try keyword is used to start a block of code that will be tested for exceptions. If an exception
occurs, the control is transferred to the corresponding catch block.
The catch keyword is used to handle exceptions thrown by a try block. It is followed by the type of
exception to be caught and the variable that will hold the exception object.
try {
} catch (ArithmeticException e) {