Java Tutorials : Create Immutable class in Java

preview_player
Показать описание
Immutability: Should not change Object values once created
//Make class as final
//Make variables as private
//Make variable as final
//No setter method
//Make Deep copy for Object

What benefits we get due to immutability:
- Saving Heap Space
- Good for HashMap Key
- Good for critical operation
- Good for Multiple thread operation. Thread safe

DO watch video for more info.

This problem is similar to following:
create immutable class in Java,
java create immutable class,
how you create immutable class in java,
java how to create immutable class,
create immutable class,
What is immutable class,
immutable class
java tutorial,
coding simplified,
java

CHECK OUT CODING SIMPLIFIED

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 300+ videos. My account is Partner Verified.

★☆★ VIEW THE BLOG POST: ★☆★

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ SEND EMAIL At: ★☆★
Рекомендации по теме
Комментарии
Автор

If there is a getter for the Engine object, then we should make sure that the getter also returns a new copy of Engine instead of just "return this.engine;" otherwise there is a chance of modifying the Engine object from outside hence violating the immutability property.
public Engine getEngine() {
return new Engine(this.engine.speed);
}

priyanka.sarkar
Автор

Thank you so much for the clear explanation. Looking in Google for better one and found this one as helpful

valuroutuharigowtam
Автор

Thank you so much for your explanation.

aravinth
Автор

What about returning defensive copy of object in getter method. Is it required or not? Only constructor is enough?

naveenkumarmaddala
Автор

Thank you. Since you created a String object in the constructor (name), shouldn't that too be a deep copy, rather than "this.name"=s;

justinblake
Автор

but bro i have a doubt here, as we are making the variables as final itself, so what effect does it make if there are setters or not. Anyway you cannot assign the values to final once declared. so using setters if we try to assign new value, we might error right?, Please explain

aloneSurvivour
Автор

@ 2:30 : How were you able to change the private id without setters?

varuntanwar
Автор

bro have some map, date object or stringbuilder any mutable object and post..

anandreddy
Автор

If there are several fields in Engine class, is it necessary to pass each value to constructor? How about using Object.clone for creating deep copy?

jainakash
Автор

@4:37 you said, the engine is copying by reference, that is not the correct term to use. As java is always passed by value. The right thing should have been, passing the value of the reference and copying the value of reference. Nice video rest.

dinkarinjosh
Автор

In order to make your Example work in the Immutable class you should change private final Engine engine to public final Engine engine; Otherwise it will not be accessible in the main class as
I wonder how come you are bale to access private object of Engine in the main method using ImmutableClass object.

rohitsharma-xtqe
visit shbcf.ru