There is a final keyword, which is used to make a reference value permanent. It means – the value can not be changed once defined. Then there is effective final variable. These variables are not declared final. But, depending on the context they can be considered as final. What do i mean from a context? you should […]
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 […]
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 […]
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 […]
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 […]