Javatpoint Logo
Javatpoint Logo

Final Object in Java

In Java, final is a keyword that ensures immutability of primitive types, methods, variables classes, etc. It is treated as non-accessible modifier. If we want to use the final keyword, we must specify it before a variable, method, and class. It restricts us to access the variable, method, and class. What if we want to make object as final? So, in this section, we will discuss the use of final keyword with object in Java. Also, we will discuss the difference between final and immutable object.

Final Object

If we use the final keyword with an object, it means that the reference cannot be changed, but its state (instance variables) can be changed.

Making an object reference variable final is a little different from a final variable. In that case object fields can be changed but the reference can't be changed. Note that it is applicable to collections, also.

When a collection marked as final, it means only its reference can't be changed but values can be added, deleted or modified.

The following code snippet demonstrates how to declare final object.

If we instantiate the class Demo, we would not able to assign other value to the attribute obj because it is final.

Let's understand the concept through a Java program.

FinalObjectExample1.java

Output:

The value is: 11

We are easily getting and setting value of a final object even if it is declared as final. But the following is not allowed.

Final Object Vs. Immutable Object

In Java, final and immutable object are not the same. There is a slight difference between them.

Final

The final keyword in Java used before a variables, methods, and class that makes the them immutable or unchangeable. The final keyword restricts the user to use variables, methods, and class. It is applicable for all the types of variables such as for instance variable, static variable, and local variable. When a variable declared as final and assigned a value, we can't change the value of the variable. The final method can't be overridden by the child class. A class that is declared final cannot be extended. In case of final object, fields can be changed but the reference can't be changed.

Note: In case of final object, the values in memory are references to the object not the actual objects.

Immutability

Immutability means once we create an object, we are not allowed to change the content of that object. If we try to change the content and the alteration is successfully done with those changes, in such a case, a new object will be created. If there are no changes in the content, the existing object will be reused.

The following Java program demonstrate the concept of final and immutability.

FinalObjectExample2.java

Output:

Final Object in Java

In the above Java program, we observe the following points:

  1. If we declare reference variable as final, it does not mean that the object is immutable in nature.
  2. In the next line of the code, we have called the append() method that successfully appends the string to the created object. If the object is immutable, the operation cannot perform.
  3. But it is executed successfully as we declare reference variable as final, it means we cannot re-assign anything to that reference variable.
  4. Hence, when we try to create a new object of the StringBuffer class then it would not be created and we will get a compile-time error.

Next TopicJava OCR





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA