Final Object in Java17 Mar 2025 | 3 min read 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 ObjectIf 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 ObjectIn Java, final and immutable object are not the same. There is a slight difference between them. FinalThe 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.ImmutabilityImmutability 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: ![]() In the above Java program, we observe the following points:
Next TopicJava OCR |
ThreadGroup in Java Java provides a convenient way to group multiple threads in a single object. In such a way, we can suspend, resume or interrupt a group of threads by a single method call. Note: Now suspend(), resume() and stop() methods are deprecated. Java thread group is implemented...
9 min read
? Java multithreading enables the concurrent operation of several threads within a program. But when several threads use the same resources, problems like inconsistent data and racial situations can occur. Java provides synchronization techniques to solve these issues. Synchronized Keyword An essential component of Java's synchronization is the synchronized...
6 min read
Hierarchical Inheritance is a type of inheritance in Java where a single parent class (base class) has multiple child classes or sub-classes. Each child class inherits the properties and behavior of the parent class independently. In the following figure, class A is a base class, and class...
5 min read
In Java, the code can be smaller or larger that depends on the functionality. If a user requires small functionality, the code will be smaller in length and easy to do testing. But if a user requires more functionality in the application, the code will be...
6 min read
The Bus Reservation System serves as a basic console application written in Java where users can view buses for booking along with reserving seats and managing active reservations. The system handles seat management effectively to provide users with an unbroken booking experience. The project implements object-oriented...
8 min read
JSON stands for JavaScript object notation, is a lightweight format for storing and transporting the data. It stores the data as the key-value pair. Most of the applications use this format for transmitting the data from the server to the web page, or vice-versa. However, we...
2 min read
Functions are used to break and divide a huge code into small chunks so that the code becomes more understandable and thus reduces the complexity of the code. Arrays are the homogenous data structures for reducing the code complexity, increasing efficiency, and reducing the execution time...
6 min read
The process of arranging even numbers at even indices and odd numbers at odd indices requires arranging numbers inside an array so that even numbers are aligned at even indices (0, 2, 4, . . . ) and odd numbers are aligned at odd indices...
19 min read
In order to read and write JSON data in Java, we use org.json library. The org.json library allow us to encode and decode JSON data in Java. The org.json class provide several important classes through which we can perform several operations on that JSON data. These...
3 min read
The zebra puzzle is a complex puzzle that requires a lot of effort and mental exercise to solve the puzzle. Sometimes is also known as Einstein's Puzzle or Einstein's Riddle because it is invented by the famous German physicist Albert Einstein. The puzzle is widely used in...
30 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India