Effective Java


Attempting to enforce noninstantiability by making a class abstract does



Download 2,19 Mb.
Pdf ko'rish
bet28/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   24   25   26   27   28   29   30   31   ...   341
Bog'liq
Effective Java

Attempting to enforce noninstantiability by making a class abstract does
not work.
The class can be subclassed and the subclass instantiated. Furthermore,
it misleads the user into thinking the class was designed for inheritance (Item 19).
There is, however, a simple idiom to ensure noninstantiability. A default construc-
tor is generated only if a class contains no explicit constructors, so 
a class can be
made noninstantiable by including a private constructor
:
// Noninstantiable utility class
public class UtilityClass {
// Suppress default constructor for noninstantiability
private UtilityClass() {
throw new AssertionError();
}
...
// Remainder omitted
}
Because the explicit constructor is private, it is inaccessible outside the class.
The 
AssertionError
isn’t strictly required, but it provides insurance in case the
constructor is accidentally invoked from within the class. It guarantees
 
the class
will never be instantiated under any circumstances. This idiom is mildly counter-
intuitive because the constructor is provided expressly so that it cannot be
invoked. It is therefore wise to include a comment, as shown earlier.
As a side effect, this idiom also prevents the class from being subclassed. All
constructors must invoke a superclass constructor, explicitly or implicitly, and a
subclass would have no accessible superclass constructor to invoke.


CHAPTER 2
CREATING AND DESTROYING OBJECTS
20
Item 5:
Prefer dependency injection to hardwiring resources
Many classes depend on one or more underlying resources. For example, a spell
checker depends on a dictionary. It is not uncommon to see such classes imple-
mented as static utility classes (Item 4):

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   24   25   26   27   28   29   30   31   ...   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