Chetan Dorle wrote:Dear All,
I have one doubt and is that "How we can make Immutable class ?"
I mean we say that string is immutable class because it creates the new object and keep old one as it is .
Will it be the case that our class be immutable after declaring it as final ?
Again one question is that "Are all wrapper classes are immutable ? "
Stephan van Hulst wrote:To make a class final, you need to make sure it's internal state can not change,
Chetan Dorle wrote:Will it be the case that our class be immutable after declaring it as final ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:You are forgetting a very important step. You need to perform defensive copying in the constructor and any accessors, if they respectively accept and return mutable types.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Santosh Kumar Nayak wrote:No parameterized constructor (only default is allowed)
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Chetan Dorle wrote:
There is no keyword or language level construct to indicate immutability (though I wish there was).
Santosh Kumar Nayak wrote:There are chances that within class Employee the object of Person class can be altered.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Amit Ghone wrote:i have a following code. let me know whether it is correct or not
Tim Driven Development | Test until the fear goes away
Alexandre Queiroz de Oliveira wrote:To make a class immutable you can turn all attributes into private, do not create any setter method, and then just use the constructor or a Builder to set the attributes.
Stephan van Hulst wrote:Welcome to CodeRanch!
Alexandre Queiroz de Oliveira wrote:To make a class immutable you can turn all attributes into private, do not create any setter method, and then just use the constructor or a Builder to set the attributes.
This is not complete. You need to make sure that the fields are immutable as well, or that you perform defensive copies in the constructors and getters.
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|