Effective Java


// Constant utility class



Download 2,19 Mb.
Pdf ko'rish
bet101/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   97   98   99   100   101   102   103   104   ...   341
Bog'liq
Effective Java

// Constant utility class
package com.effectivejava.science;
public class PhysicalConstants {
private PhysicalConstants() { } // Prevents instantiation
public static final double AVOGADROS_NUMBER = 6.022_140_857e23;
public static final double BOLTZMANN_CONST
= 1.380_648_52e-23;
public static final double ELECTRON_MASS
= 9.109_383_56e-31;
}
Incidentally, note the use of the underscore character (
_
) in the numeric liter-
als. Underscores, which have been legal since Java 7, have no effect on the values
of numeric literals, but can make them much easier to read if used with discretion.
Consider adding underscores to numeric literals, whether fixed of floating point, if
they contain five or more consecutive digits. For base ten literals, whether integral
or floating point, you should use underscores to separate literals into groups of
three digits indicating positive and negative powers of one thousand.
Normally a utility class requires clients to qualify constant names with a class
name, for example, 
PhysicalConstants.AVOGADROS_NUMBER
. If you make heavy
use of the constants exported by a utility class, you can avoid the need for qualify-
ing the constants with the class name by making use of the 
static import
facility:
// Use of static import to avoid qualifying constants
import static com.effectivejava.science.PhysicalConstants.*;
public class Test {
double atoms(double mols) {
return AVOGADROS_NUMBER * mols;
}
...
// Many more uses of PhysicalConstants justify static import
}
In summary, interfaces should be used only to define types. They should not
be used merely to export constants.


ITEM 23: PREFER CLASS HIERARCHIES TO TAGGED CLASSES
109

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   97   98   99   100   101   102   103   104   ...   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