Nothing Special   »   [go: up one dir, main page]

Skip To Content6

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 16

Skip to content

geeksforgeeks
Courses 90% Refund
Tutorials
Java
Practice
Contests

Sign In

Java Arrays
Java Strings
Java OOPs
Java Collection
Java 8 Tutorial
Java Multithreading
Java Exception Handling
Java Programs
Java Project
Java Collections Interview
Java Interview Questions
Java MCQs
Spring
Spring MVC
Spring Boot
Hibernate

Content Improvement Event


Share Your Experiences
Java Tutorial
Overview of Java
Basics of Java
Input/Output in Java
Flow Control in Java
Operators in Java
Strings in Java
Arrays in Java
OOPS in Java
Inheritance in Java
Abstraction in Java
Encapsulation in Java
Polymorphism in Java
Constructors in Java
Methods in Java
Interfaces in Java
Wrapper Classes in Java
Keywords in Java
Java Keywords
Important Keywords in Java
Super Keyword in Java
final Keyword in Java
static Keyword in Java
enum in Java
transient keyword in Java
volatile Keyword in Java
final, finally and finalize in Java
Access Modifiers in Java
Memory Allocation in Java
Classes of Java
Packages in Java
Java Collection Tutorial
Exception Handling in Java
Multithreading in Java
Synchronization in Java
File Handling in Java
Java Regex
Java IO
Java Networking
JDBC - Java Database Connectivity
Java 8 Features - Complete Tutorial
Java Backend DevelopmentCourse
final, finally and finalize in Java
Last Updated : 07 Apr, 2023
This is an important question concerning the interview point of view.

final keyword

final (lowercase) is a reserved keyword in java. We can’t use it as an identifier,


as it is reserved. We can use this keyword with variables, methods, and also with
classes. The final keyword in java has a different meaning depending upon whether
it is applied to a variable, class, or method.

final with Variables: The value of the variable cannot be changed once initialized.

class A {
public static void main(String[] args)
{
// Non final variable
int a = 5;

// final variable
final int b = 6;

// modifying the non final variable : Allowed


a++;

// modifying the final variable :


// Immediately gives Compile Time error.
b++;
}
}
If we declare any variable as final, we can’t modify its contents since it is
final, and if we modify it then we get Compile Time Error.

final with Class: The class cannot be subclassed. Whenever we declare any class as
final, it means that we can’t extend that class or that class can’t be extended, or
we can’t make a subclass of that class.

final class RR {
public static void main(String[] args)
{
int a = 10;
}
}
// here gets Compile time error that
// we can't extend RR as it is final.
class KK extends RR {
// more code here with main method
}
final with Method: The method cannot be overridden by a subclass. Whenever we
declare any method as final, then it means that we can’t override that method.

class QQ {
final void rr() {}
public static void main(String[] args)
{
}
}

class MM extends QQ {

// Here we get compile time error


// since can't extend rr since it is final.
void rr() {}
}
Note : If a class is declared as final as by default all of the methods present in
that class are automatically final, but variables are not.

// Java program to illustrate final keyword


final class G {

// by default it is final.
void h() {}

// by default it is not final.


static int j = 30;

public static void main(String[] args)


{
// See modified contents of variable j.
j = 36;
System.out.println(j);
}
}
Output:

36
finally keyword

Just as final is a reserved keyword, so in the same way finally is also a reserved
keyword in java i.e, we can’t use it as an identifier. The finally keyword is used
in association with a try/catch block and guarantees that a section of code will be
executed, even if an exception is thrown. The final block will be executed after
the try and catch blocks, but before control transfers back to its origin. finally
is executed even if try block has return statement.

// A Java program to demonstrate finally.


class Geek {
// A method that throws an exception and has finally.
// This method will be called inside try-catch.
static void A()
{
try {
System.out.println("inside A");
throw new RuntimeException("demo");
}
finally
{
System.out.println("A's finally");
}
}

// This method also calls finally. This method


// will be called outside try-catch.
static void B()
{
try {
System.out.println("inside B");
return;
}
finally
{
System.out.println("B's finally");
}
}

public static void main(String args[])


{
try {
A();
}
catch (Exception e) {
System.out.println("Exception caught");
}
B();
}
}
Output:

inside A
A's finally
Exception caught
inside B
B's finally
There are various cases when finally can be used. There are discussed below:

Case 1: Exceptions do not occur in the program

// Java program to illustrate finally in


// Case where exceptions do not
// occur in the program
class B {
public static void main(String[] args)
{
int k = 55;
try {
System.out.println("In try block");
int z = k / 55;
}

catch (ArithmeticException e) {
System.out.println("In catch block");
System.out.println("Dividing by zero but caught");
}

finally
{
System.out.println("Executes whether exception occurs or not");
}
}
}
Output:

In try block
Executes whether exception occurs or not
Here the above exception does not occur but still, finally, the block executes
since finally is meant to execute whether an exception occurs or not. The flow of
the Above Program: First it starts from the main method and then goes to try block
and in the try, since no exception occurs, the flow doesn’t go to catch block hence
flow goes directly from try to finally block.

Case 2: Exception occurs and corresponding catch block matches

// Java program to illustrate finally in


// Case where exceptions occur
// and match in the program
class C {
public static void main(String[] args)
{
int k = 66;
try {
System.out.println("In try block");
int z = k / 0;
// Carefully see flow doesn't come here
System.out.println("Flow doesn't came here");
}

catch (ArithmeticException e) {
System.out.println("In catch block");
System.out.println("Dividing by zero but caught");
}

finally
{
System.out.println("Executes whether an exception occurs or not");
}
}
}
Output:

In try block
In catch block
Dividing by zero but caught
Executes whether an exception occurs or not
Here, the above exception occurs, and the corresponding catch block is found but
still, finally, the block executes since finally is meant to execute whether an
exception occurs or not or whether the corresponding catch block is found or not.
The flow of the Above Program: First, starts from the main method and then goes to
try block, and in the try, an Arithmetic exception occurs, and the corresponding
catch block is also available, so flow goes to catch block. After that flow doesn’t
go to try block again since once an exception occurs in try block then flow doesn’t
come back again to try block. After that finally, execute since finally is meant to
execute whether an exception occurs or not or whether a corresponding catch block
is found or not.

Case 3: Exception occurs and the corresponding catch block is not found/match

// Java program to illustrate finally in


// Case where exceptions occur
// and do not match any case in the program
class D {
public static void main(String[] args)
{
int k = 15;
try {
System.out.println("In try block");
int z = k / 0;
}

catch (NullPointerException e) {
System.out.println("In catch block");
System.out.println("Dividing by zero but caught");
}

finally
{
System.out.println("Executes whether an exception occurs or not");
}
}
}
Output:

In try block
Executes whether an exception occurs or not
Exception in thread "main":java.lang.ArithmeticException:
/ by zero followed by stack trace.
Here above exception occurs and the corresponding catch block is not found/matched
but still finally block executes since finally is meant to execute whether an
exception occurs or not or whether the corresponding catch block is found/matched
or not. The flow of the Above Program: First starts from the main method and then
goes to try block and in try an Arithmetic exception occurs, and the corresponding
catch block is not available, so flow doesn’t go to catch block. After that flow
doesn’t go to try block again since once an exception occurs in try block then flow
doesn’t come back again to try block. After that finally, execute since finally is
meant to execute whether an exception occurs or not or whether the corresponding
catch block is found/matches or not.

Case 4: finally block doesn’t get execute irrespective of the exception that occurs

// Java program to illustrate finally in

// case where finally doesnt get executed


class E{
public static void main(String[] args){
try{
System.out.println("In try block");
System.exit(0);
}
catch(ArithmeticException e){
System.out.println("In catch block");
}
finally{
System.out.println("finally block");
}
}
}
Output:

In try block
Here in the above program, finally, the block doesn’t execute. There is only one
situation where finally block won’t be executed when we are using System.exit(0)
method. When we are using System.exit(0) then JVM itself shutdown, hence in this
case finally block won’t be executed. Here, the number within the parenthesis is
known as the status code. Instead of zero, we can take any integer value where zero
means normal termination, and non-zero means abnormal termination. Whether it is
zero or non-zero, there is no change in the result and the effect is the same with
respect to the program.

Application of finally block: So basically the use of finally block is resource


deallocation. This means all the resources such as Network Connections, and
Database Connections, which we opened in the try block is needed to be closed so
that we won’t lose our resources as opened. So those resources are needed to be
closed in the final block.

// Java program to illustrate


// use of finally block
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

class K {
private static final int SIZE = 10;
public static void main(String[] args)
{

PrintWriter out = null;


try {
System.out.println("Entered try statement");

// PrintWriter, FileWriter
// are classes in io package
out = new PrintWriter(new FileWriter("OutFile.txt"));
}
catch (IOException e) {
// Since the FileWriter in
// try block can throw IOException
}

// Following finally block cleans up


// and then closes the PrintWriter.

finally
{
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}
}
Output:

Entered try statement


PrintWriter not open
Note: The finally block is a key tool for preventing resource leaks. When closing a
file or otherwise recovering resources, place the code in a finally block to ensure
that resource is always recovered.

How jdk 1.7 makes usage of the final block optional?

Until jdk 1.6 finally block is like a hero i.e, it is recommended to use it for
resource deallocation but from jdk 1.7 onwards finally, the block is now optional
(however you can use it). Since the resources which we opened in the try block will
automatically get deallocated/closed when the flow of the program reaches the end
of the try block. This concept of automatic resource deallocation without using the
final block is known as the try-with-resources statement.

Finalize method
It is a method that the Garbage Collector always calls just before the
deletion/destroying of the object which is eligible for Garbage Collection, so as
to perform clean-up activity. Clean-up activity means closing the resources
associated with that object like Database Connection, Network Connection, or we can
say resource de-allocation. Remember, it is not a reserved keyword. Once the
finalized method completes immediately Garbage Collector destroys that object.
finalize method is present in the Object class and its syntax is:

protected void finalize throws Throwable{}


Since the Object class contains the finalize method, hence finalize method is
available for every Java class since Object is the superclass of all Java classes.
Since it is available for every java class hence Garbage Collector can call
finalize method on any Java object Now, the finalize method which is present in the
Object class, has an empty implementation, in our class clean-up activities are
there, then we have to override this method to define our own clean-up activities.
Cases related to finalizing method:

Case 1: The object which is eligible for Garbage Collection, that object’s
corresponding class finalize method is going to be executed

class Hello {
public static void main(String[] args)
{
String s = new String("RR");
s = null;

// Requesting JVM to call Garbage Collector method


System.gc();
System.out.println("Main Completes");
}

// Here overriding finalize method


public void finalize()
{
System.out.println("finalize method overridden");
}
}
Output:

Main Completes
Note : Here above output came only Main Completes and not “finalize method
overridden” because Garbage Collector call finalize method on that class object
which is eligible for Garbage collection. Here above we have done-> s = null and
‘s’ is the object of String class, so String class finalize method is going to be
called and not our class(i.e, Hello class). So we modify our code like->

Hello s = new Hello();


s = null;
Now our class i.e, Hello class finalize method is called.

Output:

finalize method overridden


Main Completes
So basically, Garbage Collector calls finalize method on that class object which is
eligible for Garbage collection. So if a String object is eligible for Garbage
Collection, then the String class finalize method is going to be called and not the
Hello class finalize method.

Case 2: We can call finalize method Explicitly then it will be executed just like a
normal method call, but the object won’t be deleted/destroyed

class Bye {
public static void main(String[] args)
{
Bye m = new Bye();

// Calling finalize method Explicitly.


m.finalize();
m.finalize();
m = null;

// Requesting JVM to call Garbage Collector method


System.gc();
System.out.println("Main Completes");
}

// Here overriding finalize method


public void finalize()
{
System.out.println("finalize method overridden");
}
}
Output:

finalize method overridden


//call by programmer but object won't gets destroyed.
finalize method overridden
//call by programmer but object won't gets destroyed.
Main Completes
finalize method overridden
//call by Garbage Collector just before destroying the object.
Note: As finalize is a method and not a reserved keyword, so we can call finalize
method Explicitly, then it will be executed just like normal method call, but
object won’t be deleted/destroyed.

Case 3:

Part a) If the programmer calls finalize method while executing finalize method,
some unchecked exception rises.

class Hi {
public static void main(String[] args)
{
Hi j = new Hi();

// Calling finalize method Explicitly.


j.finalize();

j = null;

// Requesting JVM to call Garbage Collector method


System.gc();
System.out.println("Main Completes");
}

// Here overriding finalize method


public void finalize()
{
System.out.println("finalize method overridden");
System.out.println(10 / 0);
}
}
Output:

exception in thread "main" java.lang.ArithmeticException:


/ by zero followed by stack trace.
So the key point is: If the programmer calls finalize method while executing
finalize method some unchecked exception rises, then JVM terminates the program
abnormally by rising an exception. So in this case, the program termination is
Abnormal.

Part b) If the garbage Collector calls finalize method while executing finalize
method, some unchecked exception rises.

class RR {
public static void main(String[] args)
{
RR q = new RR();
q = null;

// Requesting JVM to call Garbage Collector method


System.gc();
System.out.println("Main Completes");
}

// Here overriding finalize method


public void finalize()
{
System.out.println("finalize method overridden");
System.out.println(10 / 0);
}
}
Output
Main Completes
finalize method overridden
So the key point is if Garbage Collector calls finalize method while executing
finalize method some unchecked exception rises then JVM ignores that exception and
the rest of the program will be continued normally. So in this case the program
termination is Normal and not abnormal.

Important points:

There is no guarantee about the time when finalize is called. It may be called any
time after the object is not being referred anywhere (can be garbage collected).
JVM does not ignore all exceptions while executing finalize method, but it ignores
only unchecked exceptions. If the corresponding catch block is there, then JVM
won’t ignore any corresponding catch block and will be executed.
System.gc() is just a request to JVM to execute the Garbage Collector. It’s up to
JVM to call Garbage Collector or not. Usually, JVM calls Garbage Collector when
there is not enough space available in the Heap area or when the memory is low.
If you like GeeksforGeeks and would like to contribute, you can also write an
article using write.geeksforgeeks.org or mail your article to review-
team@geeksforgeeks.org.

Three 90 Challenge is back on popular demand! After processing refunds worth INR
1CR+, we are back with the offer if you missed it the first time. Get 90% course
fee refund in 90 days. Avail now!
Want to be a master in Backend Development with Java for building robust and
scalable applications? Enroll in Java Backend and Development Live Course by
GeeksforGeeks to get your hands dirty with Backend Programming. Master the key Java
concepts, server-side programming, database integration, and more through hands-on
experiences and live projects. Are you new to Backend development or want to be a
Java Pro? This course equips you with all you need for building high-performance,
heavy-loaded backend systems in Java. Ready to take your Java Backend skills to the
next level? Enroll now and take your development career to sky highs.

Rajat Rawat and Manav

73
Previous Article
volatile Keyword in Java
Next Article
Public vs Protected vs Package vs Private Access Modifier in Java
Read More
Down Arrow
Similar Reads
finalize() Method in Java and How to Override it?
The Java finalize() method of Object class is a method that the Garbage Collector
always calls just before the deletion/destroying the object which is eligible for
Garbage Collection to perform clean-up activity. Clean-up activity means closing
the resources associated with that object like Database Connection, Network
Connection, or we can say res
2 min read
FileInputStream finalize() Method in Java with Examples
Java.io.FileInputStream.finalize() method is a part of Java.io.FileInputStream
class. It ensures that the close method of the fileInputStream is called whenever
no more references of fileInputStream exist. finalize() method is annotated
@Deprecated.finalize() method is used to perform a cleanup act when no more
references exist.finalize() method mi
3 min read
Why You Need to Close the Java Streams in Finally Block?
In Java finally block is a block used to execute important and common code. The
finally block is mostly used during exception handling with try and catch to close
streams and files. The code in finally block is executed irrespective of whether
there is an exception or not. This ensures that all the opened files are properly
closed and all the runni
2 min read
Flow control in try catch finally in Java
In this article, we'll explore all the possible combinations of try-catch-finally
which may happen whenever an exception is raised and how the control flow occurs in
each of the given cases. Control flow in try-catch clause OR try-catch-finally
clause Case 1: Exception occurs in try block and handled in catch blockCase 2:
Exception occurs in try-bl
6 min read
Difference between Final and Abstract in Java
In this article, the difference between the abstract class and the final class is
discussed. Before getting into the differences, lets first understand what each of
this means. Final Class: A class which is declared with the "Final" keyword is
known as the final class. The final keyword is used to finalize the implementations
of the classes, the me
4 min read
Private and final methods in Java
When we use final specifier with a method, the method cannot be overridden in any
of the inheriting classes. Methods are made final due to design reasons. Since
private methods are inaccessible, they are implicitly final in Java. So adding
final specifier to a private method doesn't add any value. It may in-fact cause
unnecessary confusion. class B
1 min read
Static and non static blank final variables in Java
A variable provides us with named storage that our programs can manipulate. There
are two types of data variables in a class: Instance variables : Instance variables
are declared in a class, but outside a method, constructor or any block. When a
space is allocated for an object in the heap, a slot for each instance variable
value is created. Instan
5 min read
final vs Immutability in Java
final: In Java, final is a modifier that is used for class, method, and variable
also. When a variable is declared with the final keyword, its value can’t be
modified, essentially, a constant. Immutability: In simple terms, immutability
means unchanging overtime or being unable to be changed. In Java, we know that
String objects are immutable means
3 min read
Why a Constructor can not be final, static or abstract in Java?
Prerequisite: Inheritance in Java Constructor in java is a special type of method
which is different from normal java methods/ordinary methods. Constructors are used
to initialize an object. Automatically a constructor is called when an object of a
class is created. It is syntactically similar to a method but it has the same name
as its class and a
6 min read
Can We Override Final Method in Java?
A method in a subclass is said to Override a method in its superclass if that
method has a signature identical to a method in its superclass. When this method is
called by an object of the subclass, then always the Overridden version will be
called. In other words, the method which was Overridden in the superclass will
become hidden. The JVM has 2
3 min read
Article Tags :
Java
Practice Tags :
Java
three90RightbarBannerImg
course-img
215k+ interested Geeks
Master Java Programming - Complete Beginner to Advanced
Avail 90% Refund
course-img
214k+ interested Geeks
JAVA Backend Development - Live
Avail 90% Refund
course-img
30k+ interested Geeks
Manual to Automation Testing: A QA Engineer's Guide
Avail 90% Refund
geeksforgeeks-footer-logo
Corporate & Communications Address:- A-143, 9th Floor, Sovereign Corporate Tower,
Sector- 136, Noida, Uttar Pradesh (201305) | Registered Address:- K 061, Tower K,
Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh,
201305
GFG App on Play Store
GFG App on App Store
Company
About Us
Legal
Careers
In Media
Contact Us
Advertise with us
GFG Corporate Solution
Placement Training Program
Explore
Job-A-Thon Hiring Challenge
Hack-A-Thon
GfG Weekly Contest
Offline Classes (Delhi/NCR)
DSA in JAVA/C++
Master System Design
Master CP
GeeksforGeeks Videos
Geeks Community
Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial
DSA
Data Structures
Algorithms
DSA for Beginners
Basic DSA Problems
DSA Roadmap
DSA Interview Questions
Competitive Programming
Data Science & ML
Data Science With Python
Data Science For Beginner
Machine Learning Tutorial
ML Maths
Data Visualisation Tutorial
Pandas Tutorial
NumPy Tutorial
NLP Tutorial
Deep Learning Tutorial
Web Technologies
HTML
CSS
JavaScript
TypeScript
ReactJS
NextJS
NodeJs
Bootstrap
Tailwind CSS
Python Tutorial
Python Programming Examples
Django Tutorial
Python Projects
Python Tkinter
Web Scraping
OpenCV Tutorial
Python Interview Question
Computer Science
GATE CS Notes
Operating Systems
Computer Network
Database Management System
Software Engineering
Digital Logic Design
Engineering Maths
DevOps
Git
AWS
Docker
Kubernetes
Azure
GCP
DevOps Roadmap
System Design
High Level Design
Low Level Design
UML Diagrams
Interview Guide
Design Patterns
OOAD
System Design Bootcamp
Interview Questions
School Subjects
Mathematics
Physics
Chemistry
Biology
Social Science
English Grammar
Commerce
Accountancy
Business Studies
Economics
Management
HR Management
Finance
Income Tax
Databases
SQL
MYSQL
PostgreSQL
PL/SQL
MongoDB
Preparation Corner
Company-Wise Recruitment Process
Resume Templates
Aptitude Preparation
Puzzles
Company-Wise Preparation
Companies
Colleges
Competitive Exams
JEE Advanced
UGC NET
UPSC
SSC CGL
SBI PO
SBI Clerk
IBPS PO
IBPS Clerk
More Tutorials
Software Development
Software Testing
Product Management
Project Management
Linux
Excel
All Cheat Sheets
Recent Articles
Free Online Tools
Typing Test
Image Editor
Code Formatters
Code Converters
Currency Converter
Random Number Generator
Random Password Generator
Write & Earn
Write an Article
Improve an Article
Pick Topics to Write
Share your Experiences
Internships
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy
& Privacy Policy
Got It !
Lightbox

You might also like