Final, Finally, and Finalize in Java – Examples

Difference between final, finally, and finalize 

 final

A final keyword is used in Java in order to restrict the user. It is a non-access modifier that can be used in a variable, in a class, and in a method.

The final keyword introduces a type of restriction when the users try to modify the code. The words in the keyword are differentiated by an underscore and the keyword is represented by all uppercase.

The final keyword keeps the part of the code, in which it has been used, constant when it is used. The main reason that the final keyword is used in Java is to keep the value of a part of the code constant throughout the operation of the program.

Example: 1 

output: 3.14

In Java, when a final keyword is declared it a variable it stops the value of the variable from being changed. The variable is made constant when a final keyword is initiated.

When a final variable is declared by using an initializer or an assignment statement, it gets initialized. The compiler will throw compile-time error, in the case in which the final variable is not initialized.

A final variable that is a reference to an object is known as a reference variable. Likewise, a final variable that is initialized within a method or a constructor or a block is known as a local final variable.

Example: 2 – final variable

Result: Compile-time Error

A blank final variable is the type of final variable that is not initialized when it is declared. Therefore, it is also called an uninitialized final variable. A blank final variable can be initialized with the constructor and within the instance initializer block as well.

If there exist two or more than two constructors, then it is compulsory to initialize the blank final variable in all of those constructors. In the case in which the users fail to do so, the program will throw a compile-time error.

The initialization of a blank final variable only takes place within a constructor. In the situation in which, a final variable is initialized within a static block it is called a blank final static variable. Such type of final variable does not get initialized at the time of declaration.

In the case in which a method is declared with a final keyword, it is known as a final method. Such methods that are declared with a final keyword cannot be overridden. Although a final method cannot be overridden, the inheritance of such a final method is possible. Various methods of an object class become final methods in the situation in which an object class is declared with a final keyword.

Example 3: final method 

Result: Compile-time Error

When a class in Java is declared with a final keyword, it is known as a final class. A class that has been declared with a final keyword cannot be inherited in Java. The main usage of the final class is to prevent the extension of a class so that an immutable class can be created.

When a parameter is declared with a final keyword, then it is known as a final parameter. The value of such a final parameter cannot be modified. However, a constructor can never be declared as a final constructor by using the final keyword. This is because a constructor cannot be ever inherited.

Example: 4 – final class

finally

The function of the finally block in Java is to run important operations in a code such as a stream and closing connection. The finally block is used in Java in order to execute cleanup code in the programs as it runs code like closing connection and closing a file.

Syntax:

The finally block can never be used alone and can only be used after either using a try block or a catch block. Even in the case in which the exception has already been handled, the finally block is used.

Example: 1 

Output: 

Arithmetic Exception
Finally block
This is called finally block

finalize

The finalize method is a method that is invoked in Java by the garbage collector when it makes the decision that there are no more references to the object. This method is only called for one time before a garbage collection operation is going to run on an object.

Example 1 : 

Output: This is finalize.

Difference between final, finally, and finalize 

Basis Final Finally         Finalize
1.      Type It is a keyword. It is a block. It is a method.
2.      Function The final keyword is used in Java in order to restrict class, variable, and method. When the final keyword is used in a class, it restricts the class from being inherited and it restricts the variable from being modified in the case in which it is used with a variable. Similarly, when the final keyword is used with a method, it restricts the method from being overridden. The finally block is used in Java in order to execute codes whether the exception has been managed or not. The finalize method is used in Java in order to carry out the cleanup process just before the garbage collection operation is run on an object.

If you have any concern or question about Final, Finally, and Finalize please do comment. We will respond you as much as possible.

Related Posts:
Exception Handling in Java – Try, Catch & Finally
Abstract Class Vs Interface in Java With Examples
String to int in Java – Convert int to String
IO Streams in Java – InputStream and OutputStream
While Loop – Print Prime Numbers in Java
Serialization and Deserialization in Java With Examples
Collections in Java – with Examples For Beginners
Deadlock in Java – wait, notify & notifyAll

Leave a Reply

Your email address will not be published. Required fields are marked *