in Technology by
How to create an immutable class in JAVA?

1 Answer

0 votes
by

Below are some important facts about how to create an immutable class

(1)First we declare a class as final so that it cannot be extended.

(2)Second we create all fields as private so that direct access is not allowed.

(3)Third we will not provide setter methods for variables.

(4)Fourth create all mutable fields final so that their value can be assigned only once.

(5)Fifth we will initialize all fields via a constructor performing the deep copy.

(6)Sixth we will Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.

Related questions

0 votes
    How can you create Singleton class in java?...
asked Nov 7, 2020 in Education by Editorial Staff
0 votes
    In .NET 4 beta 2, there is the new Numerics namespace with struct BigInteger. The documentation states that it is an ... = BigInteger.One; BigInteger b = BigInteger.One; while (b...
asked May 24, 2022 in Education by JackTerrance
0 votes
    In .NET 4 beta 2, there is the new Numerics namespace with struct BigInteger. The documentation states that it is an ... = BigInteger.One; BigInteger b = BigInteger.One; while (b...
asked May 8, 2022 in Education by JackTerrance
0 votes
    Anyone found a good pattern for getting around immutable styles in Silverlight 2? What I mean is does ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    what is mutable and immutable data type give two example of each Select the correct answer from above options...
asked Dec 2, 2021 in Education by JackTerrance
0 votes
    How to create a RMI object in Java?...
asked May 19, 2021 in Technology by JackTerrance
0 votes
    I am wanting to find the distance between two different points. This I know can be accomplished with the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Maven command to create Java project is ____________. A. mvn archetype:generate -groupId=com.play ... artifactId=cucumber-jvm-maven -DarchetypeArtifactId=maven-archetype-quickstart...
asked Dec 13, 2022 in Technology by JackTerrance
0 votes
    Which is the correct way to create an array in JavaScript? I) var myProg = []; II) var myArray = ["C","Java","C++","Python"]; III) ... 1. I, III 2. II, III 3. I, II 4. I, II & III...
asked Feb 24, 2021 in Technology by JackTerrance
0 votes
    Which of the following class in Bootstrap is used to create a well? .wel .well .well-container .container-well...
asked Mar 2, 2021 in Technology by JackTerrance
0 votes
    Which of the following class in Bootstrap is used to create a panel? .panel .container .box .jumbotron...
asked Mar 2, 2021 in Technology by JackTerrance
0 votes
    Which of the following class in Bootstrap is used to create a badge? .tag .badge .page .flag...
asked Mar 2, 2021 in Technology by JackTerrance
...