I know that java.long.String is an immutable class.
I need to know different ways on to how create an immutable class in java.
What are the pros and cons of immutable class

Recommended Answers

An immutable class cannot change any of its values after an instance has been created.
eg You can make all its variables private, and don't write any setter methods.
Immutable classes can be very efficient becuase the code can assume that no value ever changes. The "con" is that if …

Jump to Post

All 2 Replies

An immutable class cannot change any of its values after an instance has been created.
eg You can make all its variables private, and don't write any setter methods.
Immutable classes can be very efficient becuase the code can assume that no value ever changes. The "con" is that if you do want a different value you have to create a whole new instance.

and that if you "change" the value a lot, and don't need to keep the old value, as is often the case when using Strings, you'll stuff a lot of values you don't need into your memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.