Header Ads

  • Breaking Now

    Can you change the reference of the final object?

    No the reference cannot be changed, but the data in that object can be changed.


    1. Can you give us an example

    2. Yes ! For eg.

      final SomeClass objReference = new SomeClass();

      objReference -> final reference;
      new SomeClass() -> Object on heap;

      now pointing the reference to some other new Object on heap.

      objReference = new SomeClass("anothet object ");

      which is not correct ie wrong.

      But on other hand we can modify the object which is pointed by objReference ie new SomeClass();

      eg objReference.modifyMe(); which modifies the object not the reference !


    Post Top Ad

    Post Bottom Ad