Home » Java programming language

Compare final, finally and finalize() in java



In this article, we are going to learn what are the differences between final, finally and finalize() in java?
Submitted by Preeti Jain, on December 29, 2017

final

1) 'final' is an access modifier or keyword modifier.

2) This access modifier is applicable for classes, methods and variables.

Example 1: Declare class as final

final class FinalClass{
	public static void main(String[] args){
		System.out.println("Hi,this is final class");
	}
}

Output (Description)

Whenever we declare a class as final so we need to write final modifier before class keyword.

E:\javasource>java FinalClass
Hi,this is final class

Example 2: Declare method as final

class FinalMethod{
	final void finalMethod(){
		System.out.println("Hi,this is final method");
	}
	
	public static void main(String[] args){
		FinalMethod fm = new FinalMethod();
		fm.finalMethod();
	}
}

Output (Description)

Whenever we declare a method as final so we need to write final modifier before method return type.

E:\javasource>java FinalMethod
Hi,this is final method

Example 3: Declare variable as final

class FinalVariable{
	final int a = 10;

	public static void main(String[] args){
		FinalVariable fv  = new FinalVariable();
		System.out.println("Hi,"+fv.a+" is final variable");
	}
}

Output (Description)

Whenever we declare a variable as final so we need to write final modifier before variable return type.

E:\javasource>java FinalVariable
Hi,10 is final variable

3) When a class declared as final it means creation of child class is not possible or in other words we can say final class cannot be inherited that’s why child class creation not possible.

Example: Child class creation not possible for final class

final class FinalClass{
	public static void main(String[] args){
		System.out.println("Hi,this is final class");
	}
}
class ChildClass extends FinalClass{
}

Output (Description)

Whenever we try to inherit FinalClass then we will get compile time error because we cannot create child class of final class.

E:\javasource>javac ChildClass.java
ChildClass.java:11: cannot inherit from final FinalClass
class ChildClass extends FinalClass{
1 error

4) When a method declared as final it means we cannot override the final method because final method will be as it is in child class we cannot change implementation as we already know final is final we cannot change or its implantation is fixed.

Example 1: Attempt to override parent class final method in child class.

class ParentClass{
	final void finalMethod(){
		System.out.println("Hi,this is actual final method");
	}
}
class ChildClass extends ParentClass{
	final void finalMethod(){
		System.out.println("Hi,this is overrideable final method");
	}
}

Output (Description)

Whenever we try to override parent class final method in child class then we will get compile time error.

E:\javasource>javac ChildClass.java
ChildClass.java:10: finalMethod() in ChildClass cannot override finalMethod() in
 ParentClass; overridden method is final
        final void finalMethod(){
                   ^
1 error

Example 2: Parent class final method used as it is in child class(Not Overriding).

class ParentClass{
	final void finalMethod(){
		System.out.println("Hi,this is parent class final method");
	}
}
class ChildClass extends ParentClass{
	public static void main(String[] args){
		ParentClass pc = new ParentClass();
		pc.finalMethod();
	}
}

Output (Description)

Whenever we don’t override parent class final method in child class then no compile time error parent class final method will be executed.

E:\javasource>java ChildClass
Hi,this is parent class final method.

5) When a variable declared as final then we cannot reassign the same final variable (or change the value of final variable again) because it is fixed or constant.

Example: Attempt to reassign final variable.

class FinalVariable{
	final int a = 10;
	a = 20;
	
	public static void main(String[] args){
		System.out.println("Hi,"+a+" is final variable");
	}
}

Output (Description)

Whenever we try to reassign final variable then we will get compile time error.

E:\javasource>javac FinalVariable.java
FinalVariable.java:11: cannot inherit from final FinalClass
class ChildClass extends FinalClass{
                         ^
1 error

finally

1) It is a block.

2) This finally block always associated with try catch.

Syntax:

try{
	//Risky Code
}
catch{
	// Handling Code
}
finally{
	//Resource releasing activity
}

3) It is useful in exception handling and used to maintain clean up resources (for example: closing connection object of database).

4) This block will be executed always whether exception raised or not raised and whether exception handled or not handled. Because in this block we maintain resource releasing activity so this block should be executed at anyhow.

Example 1: Exception raised.

class TryCatch
{
	public static void main(String[] args){
		try
		{	
			int a =5;
			int b =0;
			int c = a/b;
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		finally{
			System.out.println("finally block will be executed whether exception raised or not");
		}
	}
}

Output (Description)

Here exception raised in a/b statement and immediately ctrl transferred to catch block and print desired exception.

E:\javasource>java TryCatch
java.lang.ArithmeticException: / by zero
finally block will be executed whether exception raised or not

Example 2: Exception not raised.

class TryCatch
{
	public static void main(String[] args){
		try
		{	
			int a =5;
			int b =5;
			int c = a/b;
			System.out.println("no exception raised");
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
}

Output (Description)

When Exception not raised in try block then catch block will not be executed.

E:\javasource>java TryCatch
no exception raised

finalize()

  • It is a method.
  • This method should be executed by garbage collector before destroying any object to perform clean-up activities. It means finalize() method can’t be executed manually or by user.

Note: we can’t expect exact behavior of the garbage collector when garbage collector will call then finalize() method will be executed we don’t know that’s why it is highly recommended to use finally block instead of finalize() method for clean up activity.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.