Declaring object as final in java

2019-03-12 17:12发布

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.

标签: java oop final
7条回答
男人必须洒脱
2楼-- · 2019-03-12 17:29

It means 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 class A that was first assigned to it. That's what the final keyword means in this case. It does mean that you can modify the attributes of ob because it's a normal object like any other instance of A.

查看更多
forever°为你锁心
3楼-- · 2019-03-12 17:30

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

查看更多
放我归山
4楼-- · 2019-03-12 17:41

There won't be any constant(final) objects in java. Objects are created in Heap memory area. and we can modify the object anytime.
There are only constant(final) references in java :- means you can't modify the reference and re-assign it to any other object because it is constant and through out of the final reference refers to only one object which is being assigned while declaration.

Therefore :

 final A objectA = new A();
 objectA.setI(6);  

Is valid because we are changing only the object content, not the reference.

But:

   final A objectA = new A();
   objectA = new A();  

Is not valid because you are trying to modify the reference.

查看更多
迷人小祖宗
5楼-- · 2019-03-12 17:45

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();
查看更多
何必那么认真
6楼-- · 2019-03-12 17:45

In java, as opposed to C++, all objects are pointers, so a final object can be changed, it just can't point anywhere new, that is, you can't put it on the left side of the assignment operator.

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-03-12 17:47

If an object is final you can call any methods that do internal changes as usual, but you cannot reassign the reference to point to a different object. Try to do this:

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

and notice that the code won't compile.

Think of this as a constant reference.

查看更多
登录 后发表回答