November 22, 2018

How to cache hashcodes in an immutable class.

We can keep a member variable to store hashcode for the created object. And we will return that variable whenever, a user asks for hashcodes. Here, in this article, we will use a class which is an immutable class. In the below class, I use hashcode instance variable to store the hashcode. Hashcode for a given object […]

Read more
November 22, 2018

Why an Immutable class is declared final

If you read my article – How to create an immutable class – I have mentioned there,  to use final keyword to make your class immutable. But.. Why? An immutable class has only one version on all the system. Like  String class in Java. You can not create another version of it. The Behaviour of String  class is consistent on all […]

Read more
November 18, 2018

Using java streams to create immutable class

In my post – How to create immutable classes – to make lists immutable, I created a new List-Object everytime. This is the class which, we are going to modify in this tutorial. package immutable; import java.util.ArrayList; import java.util.Date; import java.util.List; final public class Person { private int age; private String name; private Date dateOfBirth; private […]

Read more
November 14, 2018

How to create a Java class, whose objects or instances are immutables

This is one of the most common interview questions. If you have more than two years of professional experience in java development, then you are going to face this question. In this article, I will show you, how to create a class, which creates an immutable objects or instances. First of all, what does immutable […]

Read more