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.

+13


source to share


7 replies


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();

      

+31


source


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).

+10


source


If an object final

, you can call any methods that make internal changes as usual, but you cannot reassign the reference to point to another object. Try this:

final A ob = new A();
ob = new A();

      

and note that the code will not compile.

Think of it as a permalink.

+3


source


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

.

+3


source


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

      

+2


source


In java, unlike C ++, all objects are pointers, so the object final

can be modified, it simply cannot specify anything new, that is, you cannot put it on the left side of the assignment operator.

+1


source


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.

+1


source







All Articles