0% found this document useful (0 votes)
3 views

Keywords Java

Uploaded by

Raja Sekhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Keywords Java

Uploaded by

Raja Sekhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

final (Used for variables, methods, and classes)

The final keyword is used in several contexts:

Final Variable: When a variable is declared as final, its value cannot be changed once
assigned. This makes it a constant.

Final Method: When a method is declared as final, it cannot be overridden by subclasses.

Final Class: When a class is declared as final, it cannot be subclassed.

2. finally (Used for exception handling)

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.

3. super (Refers to the parent class)

The super keyword is used to refer to the superclass (parent class) of the current object. It is used
for:

 Accessing superclass constructors.

 Calling superclass methods.

 Accessing superclass fields.

4. this (Refers to the current object)

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.

 Invoke the current class's methods or constructors.


5. abstract (Used for abstract classes and methods)

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..

6. extends (Used for inheritance)

The extends keyword is used to indicate that a class is inheriting from a superclass. It establishes
an is-a relationship.

7. implements (Used for interfaces)

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.

8. throw (Used to throw exceptions)

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.

10. synchronized (Used for thread synchronization)

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:

synchronized void increment() {

count++;

 Synchronized Block:

void someMethod() {

synchronized(this) {

// Thread-safe code block

The synchronized keyword helps prevent race conditions when multiple threads access shared
resources.

11. interface (Used to declare interfaces)

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).

12. enum (Used to define enums)

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.

14. catch (Used for catching exceptions)

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 {

int result = 10 / 0; // Division by zero

} catch (ArithmeticException e) {

System.out.println("Cannot divide by zero");

You might also like