Linked Questions

18 votes
7 answers
8k views

I found an article with an interesting piece of code: public class Employee { private String firstName; private String lastName; //private default constructor private Employee(...
javing's user avatar
  • 12.5k
0 votes
3 answers
2k views

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
sab's user avatar
  • 10.1k
1 vote
0 answers
40 views

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?
Mahozad's user avatar
  • 26.3k
93 votes
14 answers
64k views

How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this?
JavaUser's user avatar
  • 26.4k
36 votes
17 answers
10k views

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, ...
parkr's user avatar
  • 3,228
17 votes
8 answers
3k views

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 ...
Albert's user avatar
  • 68.9k
10 votes
8 answers
8k views

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. ...
Silent Warrior's user avatar
11 votes
4 answers
4k views

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 ...
Amol Ghotankar's user avatar
5 votes
8 answers
579 views

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; ...
rtheunissen's user avatar
  • 7,435
5 votes
4 answers
921 views

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 ...
CStreel's user avatar
  • 2,642
2 votes
4 answers
4k views

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(...
bycfly's user avatar
  • 369
3 votes
4 answers
1k views

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 ...
reza's user avatar
  • 1,518
0 votes
5 answers
3k views

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 ...
MichalB's user avatar
  • 3,361
1 vote
4 answers
536 views

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 ...
Inquisitive's user avatar
  • 7,936
0 votes
1 answer
368 views

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: { "...
user avatar

15 30 50 per page