Declaring object as final in java

final, java, oop

Solution

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

Problem

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.

Original source