Skip to content

Tag: immutability

Setter in DTO just for integration testing?

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 in order to set these values from Integration Tests, is it a good

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 would also work. Aren’t the state vars of OneValueCache themselves Immutable with final and

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 immutable, you need to use

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. Can anyone explain me what is state and mutable data? Can anyone give me examples in either JAVA or JavaScript. Answer mutable suggest anything that can change, i.e. an int In java a string is

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 immutable. Other thoughts I may be missing

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 whenever possible.

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 than String, and vice-versa? Answer Immutable means that once the constructor for an