What is the technique adopted to create an immutable class?

Sharad Jaiswal
Sharad Jaiswal

Posted On: Feb 22, 2018

 

In order to create an immutable class, follow the below steps:

  • Pronounce the class as last so it cannot be expanded.
  • Make all fields private with the goal that immediate access is not permitted.
  • Try not to give setter strategies to factors
  • Make every changeable field last with the goal that it is esteem can be doled out just once.
  • Instate every one of the fields by means of a constructor playing out a profound duplicate.
  • Perform cloning of articles in the getter techniques to restore a duplicate instead of restoring the genuine item reference.

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Java 10 Interview Questions

    What methodology can be utilized to link to a database?

    The following steps can be utilized to link to a database:..

    Java 10 Interview Questions

    Enlist some features of JDK.

    It represents a Java Development Kit...