Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview


- Declare the class as final so that it cannot be extended -



Download 7,31 Mb.
Pdf ko'rish
bet42/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   38   39   40   41   42   43   44   45   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Declare the class as final so that it cannot be extended
Make all fields as private so that direct access to them is
not allowed
Make all fields as final so that its value can be assigned
only once
Don’t provide ‘setter’ methods for variables
When the class contains a mutable object reference,
1. While initializing the field in constructor, perform
a deep copy
2. While returning the object from its getter method,
make sure to return a copy rather than the actual
object reference
Example:
We will make Employee class as immutable, but Employee class
contains a reference of Address class
Address.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\color2.png


Employee.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\color3.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\color4.png


TestImmutable.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\color5.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\color6.png


Here, after creating Employee object, the first change is done in local
address object and then we used the employee’s getter method to
access the address object and tried to change the value in it.
Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\output.png


As, you can see that the value remained the same.
If we don’t follow the rule about mutable object reference present in
the class, let’s see what will happen in that case.
Let’s change the Employee class constructor and getter method:
Employee.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\Wrong Implementation\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\Wrong Implementation\color2.png


Now, if we run our TestImmutable.java class, below is the output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
63\Wrong Implementation\output.png


Why we perform deep copy in constructor:
When you assign the actual address object in the
constructor, then remember it is storing the reference of
address object, so if you change the value in this
address object, it will reflect in the employee object
Why we don’t return original reference from the getter:
-
When you return the original address object from the getter
method then you can use the returned object reference to
change the values in employee object

Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   38   39   40   41   42   43   44   45   ...   89




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish