Effective Java


// Public class with exposed immutable fields - questionable



Download 2,19 Mb.
Pdf ko'rish
bet78/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   74   75   76   77   78   79   80   81   ...   341
Bog'liq
Effective Java

// Public class with exposed immutable fields - questionable
public final class Time {
private static final int HOURS_PER_DAY = 24;
private static final int MINUTES_PER_HOUR = 60;
public final int hour;
public final int minute;
public Time(int hour, int minute) {
if (hour < 0 || hour >= HOURS_PER_DAY)
throw new IllegalArgumentException("Hour: " + hour);
if (minute < 0 || minute >= MINUTES_PER_HOUR)
throw new IllegalArgumentException("Min: " + minute);
this.hour = hour;
this.minute = minute;
}
... // Remainder omitted
}
In summary, public classes should never expose mutable fields. It is less
harmful, though still questionable, for public classes to expose immutable fields.
It is, however, sometimes desirable for package-private or private nested classes to
expose fields, whether mutable or immutable.


CHAPTER 4
CLASSES AND INTERFACES
80
Item 17: Minimize mutability
An immutable class is simply a class whose instances cannot be modified. All of
the information contained in each instance is fixed for the lifetime of the object, so
no changes can ever be observed. The Java platform libraries contain many
immutable classes, including 
String
, the boxed primitive classes, and
BigInteger
and 
BigDecimal
. There are many good reasons for this: Immutable
classes are easier to design, implement, and use than mutable classes. They are
less prone to error and are more secure.
To make a class immutable, follow these five rules:
1.
Don’t provide methods that modify the object’s state 
(known as 
mutators
).
2.
Ensure that the class can’t be extended.
This prevents careless or malicious
subclasses from compromising the immutable behavior of the class by
behaving as if the object’s state has changed. Preventing subclassing is
generally accomplished by making the class final, but there is an alternative
that we’ll discuss later.
3.

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   74   75   76   77   78   79   80   81   ...   341




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