Can someone clarify the significance 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());
}
}
The object A is declared as final, but I can change value of this object's instance variable and also retrive the updated value. So what are the significance of declaring an object as final. I am aware about declaring primitive datatype as final, which makes that variable constant.
There won't be any constant(final) objects in java. Objects are created in Heap memory area. and we can modify the object anytime.
A class can be made final by using the final keyword. The final class cannot be inherited and so the final keyword is commonly used with a class to prevent inheritance.
We can declare Java methods as Final Method by adding the Final keyword before the method name. The Method with Final Keyword cannot be overridden in the subclasses. The purpose of the Final Method is to declare methods of how's definition can not be changed by a child or subclass that extends it.
Java For TestersA variable cannot be modified after it is declared as final. In other words, a final variable is constant. So, a final variable must be initialized and an error occurs if there is any attempt to change the value.
ob will not be able to reference any other object : final keyword.
It can not be reassigned. But you can change its internals (it is mutable, if it was originally). So this works :
final A ob = new A();
ob.setI(6)
but this doesn't :
final A ob = new A();
ob = new A();
If you indicate any variable to be final, it means that you don't want the value it contains in the memory to change. In case of primitive types, that value represented by variables are the actual values. In case of object types, the values in memory are references to the object and not the actual objects.
For example, you have:
final int x = 7;
final Object y = new Object();
You can think of the data being represented in memory this way:
+----------+----------+
| block | value |
+----------+----------+
| 1001 | 7 |
| 1002 | 2110 |
+----------+----------+
For the sake of discussion, let's leave out the details of how Java actually manages memory (because I don't know much about it either). So, in the example above, block 1001 is represented by variable 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's 7, which is the actual value, but in case of y, 2110 is just a reference to another memory location. Both cannot change but the reason primitive type variables become constants is that they represent actual values. But actually, you can say the same for object type variables too only that the constants they represent are references. So the final keyword is pretty much consistent in this regard.
So, with final variables, if they're primitive types, they will constantly represent any particular values you set them to. If they're object/reference types, they will constantly point to any object you point them to (regardless of the objects' mutability).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With