Declaring an object as final in java
Can someone clarify the meaning of the below code.
class A
{
int i = 10;
public void setI(int b)
{
i = b;
}
public int getI()
{
return i;
}
}
class Test
{
public static void main(String args[]) throws Throwable
{
final A ob = new A();
ob.setI(10);
System.out.println(ob.getI());
}
}
Object A is declared final, but I can change the value of this object instance variable and also retrieve the updated value. So what is the meaning of declaring an object as final. I am aware of declaring a primitive data type as final, which makes this variable constant.
source to share
ob will not be able to refer to any other object: the final keyword .
It cannot be reassigned. But you can change its internals (it changed if it was originally). So this works:
final A ob = new A();
ob.setI(6)
but it is not:
final A ob = new A();
ob = new A();
source to share
If you specify any final variable, it means that you don't want the value it contains in memory to change. In the case of primitive types, this value represented by variables is the actual values. In the case of object types, the values in memory are object references, not actual objects.
For example, you have:
final int x = 7;
final Object y = new Object();
You can represent data represented in memory like this:
+----------+----------+
| block | value |
+----------+----------+
| 1001 | 7 |
| 1002 | 2110 |
+----------+----------+
For purposes of discussion, let's not go into detail about how Java actually manages memory (because I don't know much about it either). Thus, in the example above, block 1001 is represented by x and 1002 by y. Both are final variables, which means that the values they represent cannot be changed. In the case of x, it is 7, which is the actual value, but in the case of y, 2110 is just a reference to another memory location. Both cannot change, but the reason primitive type variables become constants is because they represent actual values. But in fact you can say the same for object type variables, that only the constants they represent are references. Thus,the final keyword is pretty much in line with this question.
So, with final variables, if they are primitive types, they will permanently represent whatever specific values you give them. If they are objects / reference types, they will permanently point to whatever object you point to (regardless of the mutability of the objects).
source to share
This means that the following is not possible:
final A ob = new A();
ob = new A(); // This is not possible
The variable ob
will also refer to the instance of the class A
that was first assigned to it. What the keyword means in this case final
. This means that you can change the attributes ob
, because this is a normal object like any other instance A
.
source to share
Well, in the case of an object, the reference value is the address for the objects. This way the ob value will be addressed to the object created new A();
, which will be final and you cannot change its value. this means that you cannot assign a new object to this link.
You cannot write like this.
final A ob = new A();
ob= new A(); // not allowed
source to share
There won't be any persistent (final) objects in java. Objects are created in the heap memory area. and we can change the object at any time.
In java, there are only permanent (final) references: - means that you cannot change the reference and reassign it to any other object, because it is permanent and through the final reference only refers to one object that is assigned during the declaration.
Hence:
final A objectA = new A();
objectA.setI(6);
Valid because we are only modifying the content of the object, not the link.
But:
final A objectA = new A();
objectA = new A();
Invalid because you are trying to change the link.
source to share