New Apps

Articles

Tips

Events

Improve Soft Skills

/ / What is the difference between final, finally and finalize in Java ?



This is one of the most favorite question of many interviewer for Java freshers as well as for java experienced professionals. They ask this question to confuse the candidate because they spell very similar. That is the only similarity between them. They are totally different things conceptually.

In this post, we will see how final keyword, finally block and finalize() method differ from each other.

final keyword :

final is a keyword which is used to make a variable or a method or a class as “unchangeable“. In simple terms,
A variable which is declared as final, it’s value can not be changed once it is initialized.
?
1
2
3
final int i = 10;    //final variable
 
i = 20;      //Compile time error, Value can not be changed
A method declared as final can not be overridden or modified in the sub class.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SuperClass
{
    final void methodOfSuperClass()
    {
        System.out.println("final Method");
    }
}
 
class SubClass extends SuperClass
{
    void methodOfSuperClass()
    {
        //Compile time error, final method can not be overridden.
    }
}
A class declared as final can not be extended.
?
1
2
3
4
5
6
7
8
9
final class SuperClass
{
    //final class
}
 
class SubClass extends SuperClass
{
    //Compile time error, can not create a sub class to final class
}
You can read more about final keyword here

finally Block :

finally is a block which is used for exception handling along with try and catch blocks. finally block is always executed whether exception is raised or not and raised exception is handled or not. Most of time, this block is used to close the resources like database connection, I/O resources etc.
1
2
3
4
5
6
7
8
9
10
11
12
try
{
    //checking the code for exceptions
}
catch(Exception ex)
{
    //Catching the exceptions
}
finally
{
    //This block is always executed
}
You can read more about finally block here.

finalize() Method :

finalize() method is a protected method of java.lang.Object class. It is inherited to every class you create in java. This method is called by garbage collector thread before an object is removed from the memory. finalize() method is used to perform some clean up operations on an object before it is removed from the memory.
?
1
2
3
4
protected void finalize() throws Throwable
{
    //Clean up operations
}
You can read more about finalize() method here.

finally Vs finalize() :

But, there is one similarity between finally block and finalize() method. Both are used to close the resources used by the program. finally block is used to close the resources soon after their use. finalize() method is used to close the resources before an object is removed from the memory. That means if you use finalize() method to close the resources, they will remain open until an object,  which is using them, is garbage collected.
But, using finalize() method to close the resources is less recommended as it is not guaranteed that garbage collector will always call finalize() method on an object before it is removed from the memory. If it is not called, the resources will remain open. Therefore, it is always good to close the resources soon after their use using finally block.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
try
{
    //Open the resources
 
    //Use the resources
}
catch(Exception ex)
{
    //Catching the exceptions raised in the try block
}
finally
{
    //Close the resources here only
}



«
Next

Newer Post

»
Previous

Older Post

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

No comments :

Leave a Reply

Sunday, July 3, 2016

What is the difference between final, finally and finalize in Java ?



This is one of the most favorite question of many interviewer for Java freshers as well as for java experienced professionals. They ask this question to confuse the candidate because they spell very similar. That is the only similarity between them. They are totally different things conceptually.

In this post, we will see how final keyword, finally block and finalize() method differ from each other.

final keyword :

final is a keyword which is used to make a variable or a method or a class as “unchangeable“. In simple terms,
A variable which is declared as final, it’s value can not be changed once it is initialized.
?
1
2
3
final int i = 10;    //final variable
 
i = 20;      //Compile time error, Value can not be changed
A method declared as final can not be overridden or modified in the sub class.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SuperClass
{
    final void methodOfSuperClass()
    {
        System.out.println("final Method");
    }
}
 
class SubClass extends SuperClass
{
    void methodOfSuperClass()
    {
        //Compile time error, final method can not be overridden.
    }
}
A class declared as final can not be extended.
?
1
2
3
4
5
6
7
8
9
final class SuperClass
{
    //final class
}
 
class SubClass extends SuperClass
{
    //Compile time error, can not create a sub class to final class
}
You can read more about final keyword here

finally Block :

finally is a block which is used for exception handling along with try and catch blocks. finally block is always executed whether exception is raised or not and raised exception is handled or not. Most of time, this block is used to close the resources like database connection, I/O resources etc.
1
2
3
4
5
6
7
8
9
10
11
12
try
{
    //checking the code for exceptions
}
catch(Exception ex)
{
    //Catching the exceptions
}
finally
{
    //This block is always executed
}
You can read more about finally block here.

finalize() Method :

finalize() method is a protected method of java.lang.Object class. It is inherited to every class you create in java. This method is called by garbage collector thread before an object is removed from the memory. finalize() method is used to perform some clean up operations on an object before it is removed from the memory.
?
1
2
3
4
protected void finalize() throws Throwable
{
    //Clean up operations
}
You can read more about finalize() method here.

finally Vs finalize() :

But, there is one similarity between finally block and finalize() method. Both are used to close the resources used by the program. finally block is used to close the resources soon after their use. finalize() method is used to close the resources before an object is removed from the memory. That means if you use finalize() method to close the resources, they will remain open until an object,  which is using them, is garbage collected.
But, using finalize() method to close the resources is less recommended as it is not guaranteed that garbage collector will always call finalize() method on an object before it is removed from the memory. If it is not called, the resources will remain open. Therefore, it is always good to close the resources soon after their use using finally block.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
try
{
    //Open the resources
 
    //Use the resources
}
catch(Exception ex)
{
    //Catching the exceptions raised in the try block
}
finally
{
    //Close the resources here only
}


No comments:

Post a Comment

Social Impact