I couldn’t find the info I’m looking for hence posting here for suggestion and getting to know better approach. I have an immutable DTO object like: Now I’m working on an integration test with testcontainer where I want the accountOpeningDate and installmentPaidDate to be dynamic value hence…
Tag: immutability
Immutable class while using a cache
While going through JCIP, I came across the below code snippet which was considered as Immutable. However the explanation provided makes me confused. OneValueCache wouldn’t be immutable without the copyOf calls in the constructor and getter. Arrays.copyOf was added as a convenience in Java 6; clone woul…
Make immutable Java object
My goal is to make a Java object immutable. I have a class Student. I coded it in the following way to achieve immutability: My question is, what is the best way to achieve immutability for the Student class? Answer Your class is not immutable strictly speaking, it is only effectively immutable. To make it im…
Can anyone explain me what is state and mutable data?
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. http://en.wikipedia.org/wiki/Functional_programming Can anyone explain me what is state and mutable data? Can anyone give me examples in …
Immutable Type: public final fields vs. getter
I need a small Container-Class for storing some Strings which should be immutable. As String itself is an immutable type, I thought of something like that: Many people seem to object using public fields at all and use Getters instead. IMHO this would be just boilerplate in this case, because String itself is …
Why do we need immutable class?
I am unable to get what are the scenarios where we need an immutable class. Have you ever faced any such requirement? or can you please give us any real example where we should use this pattern. Answer The other answers seem too focused on explaining why immutability is good. It is very good and I use it when…
Immutable array in Java
Is there an immutable alternative to the primitive arrays in Java? Making a primitive array final doesn’t actually prevent one from doing something like I want the elements of the array to be unchangeable. Answer Not with primitive arrays. You’ll need to use a List or some other data structure:
What is meant by immutable?
What exactly does immutable mean – that is, what are the consequences of an object being mutable or immutable? In particular, why are Java’s Strings immutable? My understanding is that the StringBuilder type is something like a mutable equivalent to String. When would I use StringBuilder rather th…