Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring object as final in java

Tags:

java

oop

final

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.

like image 767
RickDavis Avatar asked Jun 29 '12 10:06

RickDavis


People also ask

Can an object be declared as final in Java?

There won't be any constant(final) objects in java. Objects are created in Heap memory area. and we can modify the object anytime.

How do you create a final class object in Java?

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.

How do you declare a final in Java?

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.

Why do we declare a variable as final in Java?

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.


2 Answers

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();
like image 177
NimChimpsky Avatar answered Oct 26 '22 21:10

NimChimpsky


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

like image 20
Psycho Punch Avatar answered Oct 26 '22 21:10

Psycho Punch