Linked Questions
19 questions linked to/from Why do we need immutable class?
18
votes
7
answers
8k
views
Java immutable classes? [duplicate]
I found an article with an interesting piece of code:
public class Employee {
private String firstName;
private String lastName;
//private default constructor
private Employee(...
0
votes
3
answers
2k
views
Why to make a class immutable? [duplicate]
Possible Duplicate:
Why do we need immutable class?
Can anyone give me an example of a real world use of immutable class in java?
What is the real purpose? For example why is String immutable
1
vote
0
answers
40
views
Why factory methods for Collections produce immutable instances? [duplicate]
Java 9 introduced new convenience factory methods for creating Collections. As an example:
List<String> list = List.of("a", "b", "c");
Why results of these methods are immutable?
93
votes
14
answers
64k
views
Immutable class?
How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this?
36
votes
17
answers
10k
views
Downsides to immutable objects in Java? [closed]
The advantages of immutable objects in Java seem clear:
consistent state
automatic thread safety
simplicity
You can favour immutability by using private final fields and constructor injection.
But, ...
17
votes
8
answers
3k
views
non-technical benefits of having string-type immutable
I am wondering about the benefits of having the string-type immutable from the programmers point-of-view.
Technical benefits (on the compiler/language side) can be summarized mostly that it is easier ...
10
votes
8
answers
8k
views
Mutable or immutable class?
I had read in some design book that immutable class improves scalability and its good practice to write immutable class wherever possible. But I think so immutable class increase object proliferation. ...
11
votes
4
answers
4k
views
String POOL in java
Java has string pool, due to which objects of string class are immutable.
But my question stands -
What was the need to make String POOL?
Why string class was not kept like other class to hold its ...
5
votes
8
answers
579
views
Should I use accessors for field values that will never change?
Let's take this class as an example:
public class Student{
private String name;
private String id;
public Student(String name, String id){
this.name = name;
this.id = id;
...
5
votes
4
answers
921
views
Why are Date structures Immutable?
I am wondering as to why Date structures & objects, like C#'s DateTime & Obj-C's NSDate have been made immutable.
Im looking for the reasoning behind this design and the benefits of making ...
2
votes
4
answers
4k
views
Instead of using Builder pattern, why can't I just create an object and set its fields directly as follows?
Why can't I just do
Animal object = new Animal();
object.setType("cat");
object.setName("Meow");
instead of
Animal.Builder objectBuilder = new Animal.Builder();
Animal object = objectBuilder.setType(...
3
votes
4
answers
1k
views
final model properties in flutter
in the flutter when you are defining a model. the convention is to define properties as final and write a copyWith for class instead of defining non-final vars and removing the copyWith method. what ...
0
votes
5
answers
3k
views
How to set up instance variables of the object in Java? [duplicate]
This question is not connected to a concrete problem but is rather a general one. In Java, when creating a class, there are actually two options how to enable access to the instance variables of that ...
1
vote
4
answers
536
views
Usefulness of immutable objects when the state of a program constantly changes
I know that immutable objects always have the same state, the state in which they are actually created. Their invariants are establised by the constructor and since their state does not change after ...
0
votes
1
answer
368
views
The optimal path in Java (tsp)
I am new to Java and trying to get the shortest path between several cities.
Start at some point (city) got through all the other cities and end at the same city
Parsing the date from JSON File:
{ "...