Java Programming Language
Table of content:
- History Of Java Programming Langauge
- Infographic For History Of Java
- What’s In The Name | History Of Java
- Key Features Of Java
- Advantages And Disadvantages Of Java
- The Version History Of Java Langauge
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is JDK?
- How To Download Java Development Kit (JDK) For Windows, MacOS, and Linux?
- Set Environment Variables In Java
- How To Install Java (JDK) On Windows 64-Bit Machine?
- How To Install Java (JDK) On Linux?
- How To Install Java (JDK) On macOS?
- How To Test Java Installation?
- How To Write Your First Java Program On Linux OS?
- Conclusion
- Frequently Asked Questions
Table of content:
- Java Programming Language | An Introduction
- 15 Key Features Of Java
- Write Once Run Anywhere (WORA) | Features Of Java
- Java Editions
- 5 New Features Of JAVA 8
- 5 New Features Of JAVA 11
- What Makes Java Popular?
- Conclusion
- Frequently Asked Questions
Table of content:
- What is Java?
- Advantages of Java
- Disadvantages of Java
Table of content:
- What Is Java Programming?
- Role Of Integrated Development Environments (IDEs) In Java Development
- 15 Best Java IDE For Developers
- In-Depth Comparison Table
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Differences Between Java And Core Java
- What Is Java?
- What Is Core Java?
- Applications Of Java
- Applications Of Core Java
- When To Use Java?
- When To Use Core Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Variables In Java Language?
- How To Declare Variables In Java Programs?
- How To Initialize Variables In Java?
- Naming Conventions For Variables In Java
- Types Of Variables In Java
- Local Variables In Java
- Instance Variables In Java
- Static Variables In Java
- Final Variables In Java
- Scope and Lifetime of Variables In Java
- Data Types Of Variables In Java (Primitive & Non-primitive)
- Java Variable Type Conversion & Type Casting
- Working With Variables In Java (Examples)
- Access Modifiers & Variables In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Identifiers In Java?
- Syntax Rules For Identifiers In Java
- Valid Identifiers in Java
- Invalid Identifiers in Java
- Java Reserved Keywords
- Naming Conventions & Best Practices For Identifiers In Java
- What Is An Identifier Expected Error In Java?
- Reasons The Identifier Expected Error Occurs
- How To Fix/ Resolve Identifier Expected Errors In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Data Types In Java?
- Primitive Data Types In Java
- Non-Primitive Data Types In Java
- Key Differences Between Primitive And Non-Primitive Data Types In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Operators In Java?
- Types Of Operators In Java
- Unary Operators In Java
- Arithmetic Operators In Java
- Assignment Operators In Java
- Relational Operators In Java
- Logical Operators In Java
- Bitwise Operators In Java
- Shift Operators In Java
- Increment & Decrement Operators In Java
- Ternary Operator In Java
- Instanceof Operator In Java
- Precedence & Associativity Of Java Operators
- Advantages & Disadvantages Of Operators In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Return Statement In Java?
- Use Cases Of Return Statements In Java
- Returning A Value From A Method In Java
- Returning A Class Object In Java
- Returning Void (No Value) In Java
- Advantages Of Using Return Statements In Java
- Limitations Of Using Return Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Keywords In Java?
- List Of Keywords In Java
- Detailed Overview Of Java Keywords With Examples
- What If When Keywords In Java Are Used As Variable Names?
- Difference Between Identifiers & Keywords In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Abstract Keyword In Java?
- Use Of Abstract Keyword In Java
- Abstract Methods In Java
- Abstract Classes In Java
- Advantages Of Abstract Keyword In Java
- Disadvantages Of Abstract Keyword In Java
- Abstract Classes Vs. Interfaces In Java
- Real-World Applications Of Abstract Keyword
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is throws Keyword In Java?
- How Does The throws Keyword Work?
- Throwing A Checked Exception Using throws In Java
- Throwing Multiple Exceptions Using throws In Java
- Throwing A Custom Exception Using throws In Java
- When To Use The throws Keyword In Java
- Difference Between throw and throws Keyword In Java
- Best Practices For Using The throws Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Volatile Keyword In Java?
- How Does Volatile Keyword In Java Work?
- Using Volatile Keyword In Java To Control Thread Execution
- Using Volatile Keyword In Java To Signal Between Multiple Threads
- Difference Between Synchronization And Volatile Keyword
- Common Mistakes And Best Practices While Using Volatile Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Super Keyword In Java
- Super Keyword In Java With Instance Variables
- Super Keyword In Java With Method Overriding
- Super Keyword In Java With Constructor Chaining
- Applications Of Super Keyword In Java
- Difference Between This And Super Keyword In Java
- Advantages Of Using Super Keyword In Java
- Limitations And Considerations Of Super Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding This Keyword In Java
- Uses Of This Keyword In Java
- Using This Keyword For Referencing Instance Variables
- Using This Keyword For Invoking A Constructor
- Using This Keyword For Invoking A Method
- Using This Keyword With Getters And Setters
- Difference Between This And Super Keyword In Java
- Best Practices For Using This Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is new Keyword In Java?
- Uses Of The new Keyword In Java
- Memory Management With new Keyword In Java
- Example 1: Creating An Object Of A Class Using new Keyword In Java
- Example 2: Creating An Array Using The new Keyword In Java
- Best Practices For Using new Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Transient Keyword In Java?
- Real-Life Example Of The Transient Keyword In Java
- When To Use The Transient Keyword In Java
- Example 1: Effect Of Transient Keyword On Serialization In Java
- Example 2: Skipping Sensitive Data During Serialization With Transient Keyword In Java
- Using Transient With Final Keyword In Java
- Using Transient With Static Keyword
- Difference Between Transient And Volatile Keyword In Java
- Advantages And Disadvantages Of Transient Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Static Keyword In Java?
- Characteristics Of Static Keyword In Java
- Static Variables In Java
- Static Method In Java
- Static Blocks In Java
- Static Classes In Java
- Static Variables Vs Instance Variables In Java
- Advantages Of Static Keyword In Java
- Disadvantages Of Static Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Static Method In Java?
- Use Cases Of Static Method In Java
- Using Static Method In Java To Create A Utility Class
- Using Static Method In Java To Implement The Singleton Design Pattern
- Difference Between Static And Instance Methods In Java
- Limitations Of Static Method In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Final Keyword In Java
- Final Variables In Java
- Final Methods In Java
- Final Classes In Java
- Difference Between Static And Final Keyword In Java
- Uses Of Final Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Difference Between final, finally, And finalize In Java
- What Is final Keyword In Java?
- What Is finally Keyword In Java?
- What Is finalize Keyword In Java?
- When To Use Which Keyword In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The extends Keyword In Java?
- Use Of extends Keyword In Java
- Using Java extends To Implement Single Inheritance
- Using Java extends With Interfaces (Default Methods)
- Overriding Using extends Keyword In Java
- Difference Between extends And implements In Java
- Real World Applications Of Extends Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Decision Making Statement In Java?
- If Statement In Java
- If-Else Statement In Java
- Else-If Ladder In Java
- Switch Statement In Java
- Ternary/Conditional Operator (?:) In Java
- Best Practices For Writing Decision Making Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Control Statements in Java?
- Decision-Making Control Statements In Java
- Looping Control Statements In Java
- Jump (Branching) Control Statements In Java
- Application Of Control Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Break Statement In Java?
- Working Of The Break Statement In Java
- Using Java Break Statement With Loops
- Using Java Break Statement With Switch Statement
- Using Java Break Statement With Infinite Loops
- Common Pitfalls While Using Break Statements In Java
- Best Practices For Using The Break Statement In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Switch Statement In Java?
- Working Of The Switch Statement In Java
- Example Of Switch Statement In Java
- Java Switch Statement With String
- Java Nested Switch Statements
- Java Enum In Switch Statement
- Java Wrapper Classes In Switch Statements
- Uses Of Switch Statement In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Syntax Of main() Method In Java
- public Specifier – Main Method In Java
- static Keyword – Main Method In Java
- void Return Type Of Main Method In Java
- The main Identifier – Main Method In Java
- String[] args In Main Method In Java
- The Role Of Java Virtual Machine (JVM)
- Running Java Programs Without The Main Method
- Variations In Declaration Of Main Method In Java
- Overloading The Main Method In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Method Overriding In Java?
- Example Of Method Overriding In Java
- Ideal Use Cases Of Method Overriding In Java
- Rules For Method Overriding In Java
- Super Keyword & Method Overriding In Java
- Constructor & Method Overriding In Java
- Exception Handling In Method Overriding In Java
- Access Modifiers In Method Overriding In Java
- Advantages & Disadvantages Of Method Overriding In Java
- Difference Between Method Overloading Vs. Method Overriding In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Method Overloading In Java?
- Different Ways Of Method Overloading In Java
- Overloading The main() Method In Java
- Type Promotion & Method Overloading In Java
- Null Error & Method Overloading In Java
- Advantages Of Method Overloading In Java
- Disadvantages Of Method Overloading In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Difference Between Overloading And Overriding In Java (Comparison Table)
- What Is Method Overloading In Java?
- What Is Method Overriding In Java?
- Key Differences Between Overloading & Overriding In Java Explained
- Difference Between Overloading And Overriding In Java Code Example
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A One-Dimensional Array In Java?
- Key Characteristics Of One-Dimensional Arrays In Java
- Declaration Of One-Dimensional Array In Java
- Initialization Of One-Dimensional Array In Java
- Common Operations On One-Dimensional Array In Java
- Advantages Of One-Dimensional Arrays In Java
- Disadvantages Of One-Dimensional Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Multidimensional Array In Java?
- Difference Between Single-Dimensional And Multidimensional Arrays In Java
- Declaring Multidimensional Arrays In Java
- Initializing Multidimensional Arrays In Java
- Accessing And Manipulating Elements In Multidimensional Arrays In Java
- Working Of Multidimensional Arrays With Jagged Arrays In Java
- Why Use Multidimensional Arrays In Java?
- Limitations Of Multidimensional Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Jagged Arrays In Java?
- Comparison With Regular Multi-Dimensional Arrays
- Declaring Jagged Arrays In Java
- Initialization Of Jagged Arrays In Java
- Printing Elements Of A Jagged Array In Java
- Accessing And Modifying Elements Of A Jagged Array In Java
- Advantages Of Jagged Arrays In Java
- Disadvantages Of Jagged Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Array Of Objects In Java?
- Declare And Initialize An Array Of Object In Java
- Example Of An Array Of Objects In Java
- Sorting An Array Of Objects In Java
- Passing Arrays Of Objects To Methods In Java
- Returning Arrays Of Objects From Methods In Java
- Advantages Of Arrays Of Objects In Java
- Disadvantages Of Arrays Of Objects In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Dynamic Array In Java?
- Why Use Dynamic Array In Java?
- What Is The Size And Capacity Of A Dynamic Array In Java?
- How To Create A Dynamic Array In Java?
- Managing Dynamic Data Input In Java
- Storing And Processing Real-Time Data In Java
- Use Cases Of Dynamic Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Return An Array In Java?
- How To Return An Array In Java
- Example 1: Returning An Array Of First N Squares
- Example 2: Doubling the Values of an Array
- Common Scenarios For Returning Arrays In Java
- Points To Remember When Returning Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding ArrayList In Java
- Differences Between Arrays And ArrayList In Java
- Returning An ArrayList In Java
- Common Use Cases For Returning An ArrayList In Java
- Pitfalls To Avoid When Returning An ArrayList In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Thread In Java?
- Thread Vs Process
- What is a Thread Life Cycle In Java?
- What Are Thread Priorities?
- Creating Threads In Java
- Java Thread Methods
- Commonly Used Constructors In Thread Class
- Thread Synchronization In Java
- Common Challenges Faced While Using Threads In Java
- Best Practices For Using Threads In Java
- Real-World Applications Of Threads In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Multithreading In Java
- Methods Of Multithreading In Java (Examples)
- Difference Between Multithreading And Multitasking In Java
- Handling Exceptions In Multithreading
- Best Practices For Multithreading In Java
- Real-World Use Cases of Multithreading In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Thread Priority In Java?
- Built-In Thread Priority Constants In Java
- Thread Priority: Setter & Getter Methods
- Limitations Of Thread Priority In Java
- Best Practices For Using Thread Priority In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Thread Synchronization In Java?
- The Need For Thread Synchronization In Java
- Types Of Thread Synchronization In Java
- Mutual Exclusion In Thread Synchronization In Java
- Coordination Synchronization (Thread Communication) In Java
- Advantages Of Thread Synchronization In Java
- Disadvantages Of Thread Synchronization In Java
- Alternatives To Synchronization In Java
- Deadlock And Thread Synchronization In Java
- Real-World Use Cases Of Thread Synchronization In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Daemon Thread In Java?
- User Threads Vs. Daemon Threads In Java
- Methods For Daemon Threads In The Thread Class
- Creating Daemon Threads In Java
- Checking The Daemon Status Of A Thread
- Exceptions In Daemon Threads
- Limitations Of Daemon Threads In Java
- Practical Applications Of Daemon Threads In Java
- Common Mistakes To Avoid When Working With Daemon Threads In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Do Threads Need To Communicate?
- Understanding Inter Thread Communication In Java
- The wait() Method In Inter-Thread Communication
- The notify() Method In Inter-Thread Communication
- The notifyAll() Method In Inter-Thread Communication
- Difference Between wait() And sleep() Methods In Java
- Best Practices For Inter Thread Communication In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The Factorial Concept
- Approaches To Implementing Factorial In Java
- Find Factorial In Java Using Iterative Approach (Using a Loop)
- Find Factorial In Java Using Recursive Approach
- Complexity Analysis Of Factorial Programs In Java
- Applications Of Factorial Program In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The Leap Year Concept
- Approach To Check A Leap Year In Java
- Alternative Approach To Check A Leap Year In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Difference Between JDK, JRE, and JVM?
- What Is JVM (Java Virtual Machine)?
- What Is JRE (Java Runtime Environment)?
- What Is JDK (Java Development Kit)?
- Understanding The Difference Between JDK, JRE, And JVM
- Comparison Table For Difference Between JDK, JRE, And JVM
- Conclusion
- Frequently Asked Questions
Table of content:
- Difference Between Abstraction And Encapsulation In Java
- Understanding Abstraction In Java
- Understanding Encapsulation In Java
- When To Use Abstraction And Encapsulation?
- Conclusion
- Frequently Asked Questions
Table of content:
- Differences Between Abstract Class And Interface In Java
- What Is An Abstract Class In Java?
- What Is An Interface In Java?
- When To Use An Abstract Class?
- When To Use Interface?
- Compatibility Between Abstract Class And Interface In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Error Vs. Exception In Java
- What Is Error In Java?
- What Is Exception In Java?
- Best Practices For Handling Exceptions In Java
- Why Errors Should Not Be Handled In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Differences: Java Vs. JavaScript
- What Is Java?
- What Is JavaScript?
- Difference Between Java And JavaScript Explained
- Conclusion
- Frequently Asked Questions
Table of content:
- Brief Introduction To C++
- Brief Introduction To Java
- Difference Between C++ and Java
- Overview & Features Of C++ Language
- Overview & Features of Java Language
- Example of C++ and Java Program
- Key Difference Between C++ And Java Explained
- Similarities Between Java Vs. C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Basic Java interview questions and answers
- Intermediate Java interview questions and answers
- Advanced Java interview questions and answers
Table of content:
- Difference between core Java and advanced Java
- Important Core Java Questions
- Tips for Preparing for Core Java
Final, Finally & Finalize In Java | 15+ Differences With Examples

In Java, understanding the differences between final, finally, and finalize is crucial as each serves a distinct purpose, despite their similar names. The final keyword is used primarily for constant values, preventing inheritance, and prohibiting method overriding. The finally block is related to exception handling, allowing code to execute after a try-catch block regardless of whether an exception occurs.
Meanwhile, finalize() is a method in the Object class, invoked by the garbage collector before an object is discarded. In this article, we will explore each in detail, examining their roles, usage, and how they contribute to Java programming best practices.
Key Difference Between final, finally, And finalize In Java
Given below are the key differences between final, finally, and finalize in Java programming:
Feature |
final |
finally |
finalize() |
Type |
Keyword |
Block |
Method |
Purpose |
Used to restrict class inheritance, prevent method overriding, and create constants |
Ensures that essential code runs after try-catch, regardless of exceptions |
Allows custom cleanup just before an object is garbage-collected |
Usage Context |
- Classes (final class ClassName {}): Prevents subclassing. - Methods (final void methodName() {}): Prevents overriding in subclasses. - Variables (final int VALUE = 10;): Creates a constant. |
Placed after try and catch blocks (try {...} catch {...} finally {...}) to ensure cleanup, especially for resources |
Defined in classes for cleanup purposes but rarely needed with modern resource management practices |
Execution Timing |
During compilation or runtime as it restricts inheritance and reassignment |
Executes immediately after try-catch, even if an exception is thrown |
Called by the garbage collector before object deletion |
Return Value |
Not applicable |
Not applicable |
Returns void (no return value) |
Common Usage |
- Constant values for readability and maintainability. - Security in code by restricting inheritance and modification. |
To release resources such as file streams or network connections in case of exceptions |
Rarely used today; try-with-resources or explicit resource handling is preferred |
Override Capability |
Cannot be overridden |
Not applicable |
Can be overridden but often discouraged due to unpredictable timing |
Memory Management |
Doesn't directly impact memory management but enforces structure |
Doesn't impact memory management |
May impact memory if misused; can slow down garbage collection |
Impact on Performance |
No direct impact; helps optimize by preventing overrides |
Minimal; typically used for resource cleanup |
Slows down garbage collection; not ideal for performance-sensitive code |
Impact on Inheritance |
Prevents inheritance (classes and methods) or reassignment (variables) |
Not relevant to inheritance |
Does not impact inheritance but is inherited from Object class |
Example |
java<br>final int CONSTANT = 10;<br> |
java<br>try { /* code */ } finally { /* cleanup code */ }<br> |
java<br>@Override protected void finalize() throws Throwable { /* cleanup code */ }<br> |
Associated Best Practices |
Use for immutability and secure class design |
Preferred for cleanup, but try-with-resources is often better |
Generally avoid; finalize() is deprecated since Java 9 |
Garbage Collection Impact |
No direct impact on garbage collection |
Not directly related to garbage collection |
Directly affects garbage collection timing if overridden |
Thread Safety |
Helps in creating immutable objects which support thread safety |
Typically thread-safe as it runs after try-catch |
Can create issues in multithreading; should not be relied upon for resource management |
Error Handling Relationship |
Unrelated to error handling |
Directly associated with error handling; ensures cleanup |
Not directly related to error handling but impacts cleanup if object has unreleased resources |
Notes |
- Ideal for constants, secure inheritance structure. - Use with caution in class design as it limits extensibility. |
- Use to ensure resource cleanup. - Works well with traditional try-catch but use try-with-resources where possible. |
- Deprecated and replaced by explicit resource management. - Avoid relying on it for critical cleanup. |
We will now discuss each one of them in detail, along with code examples in the sections ahead.
Explore this amazing course and master all the key concepts of Java programming effortlessly!
What Is final Keyword In Java?
The final keyword in Java is a modifier that can be applied to variables, methods, and classes to provide various types of restrictions. The final keyword's primary purpose is to enforce immutability and prevent changes in specific components of Java code.
Syntax Of final Keyword In Java
We can use the final keyword in Java in three different ways as follows:
1. Using final With Variables
A final variable in Java is a constant, meaning its value cannot be changed after it is initialized.
final dataType variableName = value;
Here:
- final: Modifier that makes the variable constant, prohibiting any reassignment after the initial assignment.
- dataType: Specifies the type of data (e.g., int, String) the variable will hold.
- variableName: The name of the variable(or identifier), which should follow standard naming conventions.
- =: Assignment operator used to set the initial and only value of the final variable.
- value: The fixed value assigned to the variable, which cannot be changed later in the code.
2. Using final With Methods
A final method cannot be overridden by subclasses. This ensures that the method's implementation remains unchanged in the inheritance hierarchy.
final returnType methodName(parameters) {
// method body
}
Here:
- final: Modifier that prevents the method from being overridden in any subclass.
- returnType: Specifies the type of value the method returns (e.g., int, void, String).
- methodName: The name of the method, following Java’s naming conventions.
- parameters: Input parameters for the method, enclosed in parentheses; they define the data the method can accept.
3. Using final With Classes
A final class cannot be subclassed, meaning it cannot be extended. This is useful when you want to prevent inheritance and ensure the class implementation remains unchanged.
final class ClassName {
// class body
}
Here:
- final: Modifier that prevents the class from being subclassed or extended by other classes.
- class: Keyword used to define a class in Java.
- ClassName: The name of the class, following Java's naming conventions.
Let’s now look at a code example to understand the working of final keyword in Java-
Code Example:
// Using final with variable
final class FinalDemo { // Final class, cannot be subclassed
// final variable, can only be initialized once
final int MAX_ATTEMPTS = 3;
// final method, cannot be overridden by subclasses
public final void showMaxAttempts() {
System.out.println("Max attempts allowed: " + MAX_ATTEMPTS);
}
public static void main(String[] args) {
FinalDemo demo = new FinalDemo();
// Display the final variable value
demo.showMaxAttempts();
// Uncommenting the next line will cause an error
// demo.MAX_ATTEMPTS = 5; // Error: cannot assign a value to final variable
}
}
Output (Rename code file name to FinalDemo.java):
Max attempts allowed: 3
Explanation:
In the above code example-
- We start by defining a final class named FinalDemo. Declaring the class as final means it cannot be subclassed, which prevents any extension or modification to its structure in future classes.
- Inside the FinalDemo class, we declare a final variable called MAX_ATTEMPTS, initialized with the value 3. Since it's final, this variable can only be set once and cannot be changed later in the code.
- We define a method showMaxAttempts(), marked as final. Using final here ensures that this method cannot be overridden by any subclass, adding an additional layer of security to preserve the intended functionality.
- In the main method, we create an instance of FinalDemo called demo.
- We call demo.showMaxAttempts(), which outputs the value of MAX_ATTEMPTS to the console. This helps us see that the final variable holds the correct value.
- An important note: if we try to change the value of MAX_ATTEMPTS after it's initialized (like with demo.MAX_ATTEMPTS = 5;), it will cause a compilation error. Since MAX_ATTEMPTS is final, its value is locked and cannot be modified, preserving data integrity.
What Is finally Keyword In Java?
In Java, the finally keyword is used to define a block of code that will always execute after a try block, regardless of whether an exception was thrown or handled. The finally block is typically used for resource cleanup tasks, such as closing files or releasing database connections, ensuring that resources are freed even if an exception occurs.
Syntax Of finally Keyword In Java
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Exception handling code
} finally {
// Code that will always execute
}
Here:
- try: Block that contains code that may throw an exception. The code inside this block is monitored for exceptions.
- catch: Block that handles the exception if one is thrown in the try block. It catches specific exceptions (like ExceptionType) and provides a way to handle them.
- (ExceptionType e): Defines the type of exception that is caught (ExceptionType) and the variable (e) used to reference the caught exception.
- finally: Block that contains code that will always be executed, regardless of whether an exception occurred in the try block or not.
Code Example:
public class FinallyExample {
public static void main(String[] args) {
try {
System.out.println("Inside try block");
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e);
} finally {
System.out.println("Finally block executed.");
}
}
}
Output (Rename code file name to FinallyExample.java):
Inside try block
Exception caught: java.lang.ArithmeticException: / by zero
Finally block executed.
Explanation:
In the above code example-
- We define a class named FinallyExample, with the main method as the entry point of the program.
- Inside the main method, we begin with a try block. This block contains code that could potentially throw an exception.
- In our case, we attempt to divide 10 by 0, which is not allowed in Java and will throw an ArithmeticException.
- When the exception is thrown, the catch block immediately handles it. Here, we catch the ArithmeticException and print a message, "Exception caught," followed by the exception details. This ensures that our program handles the error gracefully without crashing.
- Following the try and catch blocks, we have a finally block. Regardless of whether an exception occurs or not, the finally block will execute, ensuring that any essential cleanup or final actions are taken. In this example, it prints "Finally block executed."
- The key takeaway is that the finally block provides a way to run important code no matter the outcome of the try and catch blocks, making it reliable for cleanup operations or mandatory statements.
What Is finalize Keyword In Java?
The finalize keyword in Java is used to define a method, finalize(), that is called by the garbage collector before an object is destroyed. The purpose of the finalize() method is to provide a final opportunity for an object to perform cleanup operations before it is removed from memory. This method belongs to the Object class and can be overridden in custom classes to release resources or handle cleanup tasks.
Syntax Of finalize Keyword In Java
protected void finalize() throws Throwable {
// Cleanup code
}
Here:
- protected: Access modifier that makes the finalize() method accessible within the same package and by subclasses.
- void: Specifies that the finalize() method does not return any value.
- finalize(): The method name, which is a special method called by the garbage collector before an object is removed from memory.
- throws Throwable: The parentheses indicate that the method can throw exceptions (in this case, Throwable) during its execution, which must be handled or declared.
Code Example:
public class FinalizeExample {
// Constructor
public FinalizeExample() {
System.out.println("Object created.");
}
// Overriding the finalize() method
@Override
protected void finalize() throws Throwable {
try {
System.out.println("finalize() method called for cleanup.");
} finally {
// Calling the superclass finalize method
super.finalize();
}
}
public static void main(String[] args) {
FinalizeExample example = new FinalizeExample();
example = null; // Dereferencing the object
// Requesting garbage collection
System.gc();
System.out.println("End of main method.");
}
}
Output (Rename code file name to FinalizeExample.java):
Object created.
End of main method.
finalize() method called for cleanup.
Explanation:
In the above code example-
- We define a class named FinalizeExample with a constructor that prints "Object created." whenever an instance of this class is created, allowing us to observe object creation.
- Inside this class, we override the finalize() method. The finalize() method is called by the garbage collector when it's about to reclaim the object's memory. Here, we include a print statement in finalize() to indicate when cleanup actions are performed.
- In the finalize() method, we use a try-finally block. This ensures that our custom cleanup code executes first, followed by the super.finalize() call, which invokes the superclass's finalize() method to complete any further necessary cleanup.
- In the main method, we create an instance of FinalizeExample, which triggers the constructor and displays "Object created."
- We then set example to null, effectively dereferencing it so that the object becomes eligible for garbage collection.
- To prompt garbage collection, we call System.gc(). This request isn't guaranteed to trigger garbage collection immediately, but if it does, finalize() will be called on the object marked for cleanup.
- Finally, we print "End of main method." to indicate the main method's end. This allows us to see that even as the program finishes, finalize() may still execute if garbage collection occurs, ensuring our cleanup actions are complete.
Sharpen your coding skills with Unstop's 100-Day Coding Sprint and compete now for a top spot on the leaderboard!
When To Use Which Keyword In Java?
When working with Java, knowing when to use each of the keywords final, finally, and finalize helps ensure your code is efficient, maintainable, and optimized for resource management. Here’s a guide on when to use each:
When To Use final Keyword In Java?
The final keyword is best used to enforce immutability, control inheritance, and secure methods or variables against modification.
- For Constants: Use final with variables to create constants, ensuring they are immutable and can’t be changed after initialization. For Example-
final int MAX_CONNECTIONS = 10;
- To Prevent Inheritance: Use final with a class when you don’t want it to be subclassed. This is useful for classes like utility classes or critical classes that should not be modified by subclasses. For Example-
public final class DatabaseUtils { ... }
- To Prevent Method Overriding: Use final with methods when you need to secure their behavior and ensure subclasses can’t override them, preserving core functionality. For Example-
public final void displayDetails() { ... }
- For Immutable Objects: Use final with object references to ensure the reference cannot point to a different object after assignment, promoting immutability in your code.
When To Use finally Keyword In Java?
The finally block is ideal for resource management and guaranteeing the execution of critical code after a try-catch block.
- For Resource Cleanup: Use finally to ensure that resources (e.g., files, database connections) are properly closed or released, regardless of whether an exception is thrown. For Example-
try {
FileInputStream file = new FileInputStream("file.txt");
// Code to process file
} finally {
file.close(); // Ensures file is closed
}
- To Ensure Code Execution: Use finally when you have code that must execute even if an exception is thrown, such as logging, releasing locks, or resetting values.
When To Use finalize Keyword In Java?
The finalize keyword is used to define a finalize() method for cleanup before garbage collection, though it’s deprecated due to unreliable behavior and performance issues. Alternatives like try-with-resources are recommended instead.
- For Last-Resort Cleanup: Use finalize() only if absolutely necessary for non-Java resources, such as native code objects, where other cleanup methods are infeasible. For Example-
@Override
protected void finalize() throws Throwable {
// Last-minute cleanup code
}
- When No Other Options Are Available: Since finalize() can be unpredictable, it’s best avoided unless there’s no alternative. It can be useful in legacy systems, but using try-with-resources or finally blocks is strongly preferred.
To Summarize
Keyword |
Best Used For |
Examples |
final |
Constants, securing classes/methods, immutability |
Constants, utility classes, critical methods |
finally |
Resource management, guaranteed execution |
File or database cleanup, logging, releasing resources |
finalize |
Deprecated cleanup before garbage collection |
Rare cases of unmanaged resources, legacy systems |
Are you looking for someone to answer all your programming-related queries? Let's find the perfect mentor here.
Conclusion
Understanding the differences between final, finally, and finalize in Java is crucial for writing clean, maintainable, and efficient code. Each keyword serves a unique purpose:
- The final ensures immutability, prevents modifications, and secures classes, methods, or variables against unwanted changes.
- The finally guarantees that critical code, such as resource cleanup, is executed regardless of exceptions, ensuring reliability in exception handling.
- The finalize, though deprecated, was historically used for last-minute object cleanup before garbage collection, but modern alternatives like try-with-resources are now preferred.
By using these keywords appropriately, we can enhance the clarity, performance, and safety of our Java programs, aligning with best practices for object-oriented programming.
Frequently Asked Questions
Q. What happens if I try to modify a final variable in Java?
Attempting to modify a final variable after it has been initialized results in a compilation error. final variables can be assigned a value only once, making them immutable. If the final variable is a reference to an object, you cannot reassign the reference to point to a different object, although the object's internal state may still be altered.
Q. Can I use the finally block without a try block in Java?
No, a finally block cannot exist without a try block. It is designed to follow a try or try-catch structure, where it provides a place for code that must run regardless of whether an exception is thrown. This structure helps ensure that critical cleanup or release of resources (like closing files or database connections) is always performed.
Q. Why is finalize() deprecated, and what should I use instead?
finalize() is deprecated because it has unpredictable execution timing and can lead to performance issues, as the garbage collector may not call it promptly. In place of finalize(), Java provides more reliable alternatives like try-with-resources (Java 7 and later) for automatic resource management or explicitly closing resources in finally blocks. These approaches offer more control and ensure that resources are released as expected.
Q. Can a final method be overloaded in Java?
Yes, a final method can be overloaded. Overloading is based on different parameter lists and does not involve overriding the method’s existing functionality. A final method, however, cannot be overridden in any subclass, as marking it final prevents subclasses from altering its behavior.
Q. What is the purpose of finally when we use try-with-resources?
With try-with-resources, resources are automatically closed at the end of the try block, reducing the need for a finally block specifically for cleanup. However, finally can still be useful for other actions that must be performed regardless of whether an exception occurs, such as logging or resetting variables, even though resources are automatically handled.
Q. Can a final class in Java implement interfaces?
Yes, a final class can implement one or more interfaces. Declaring a class as final only prevents it from being extended by other classes; it does not restrict it from implementing interfaces. For example, public final class MyFinalClass implements MyInterface { ... } is valid.
Q. What are some best practices for using final, finally, and finalize?
Some of the best practices for using final, finally, and finalize are as follows:
- final: Use final for variables that should not change, such as constants, and to secure methods or classes where modifications could compromise functionality.
- finally: Reserve finally for critical cleanup actions, like closing resources, especially if using older versions of Java that lack try-with-resources.
- finalize: Avoid finalize() in favor of more reliable resource management practices; it is deprecated and may lead to unpredictable behavior.
With this, we conclude our discussion on the key differences between final, finally, and finalize. Here are a few other topics that you might be interested in reading:
- 15 Popular JAVA Frameworks Use In 2023
- What Is Scalability Testing? How Do You Test Application Scalability?
- Top 50+ Java Collections Interview Questions
- 10 Best Books On Java In 2024 For Successful Coders
- Difference Between Java And JavaScript Explained In Detail
- Top 15+ Difference Between C++ And Java Explained! (+Similarities)
I’m a Computer Science graduate with a knack for creative ventures. Through content at Unstop, I am trying to simplify complex tech concepts and make them fun. When I’m not decoding tech jargon, you’ll find me indulging in great food and then burning it out at the gym.
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.

Subscribe
to our newsletter
Esuru Pooja.C 4 hours ago
Ankita Das 2 days ago
Sunita Bhat 3 days ago
Mihir Kumar Ranjan 1 week ago