— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet291/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   287   288   289   290   291   292   293   294   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

you are here
4
307
Even more Statics!... static imports
New to Java 5.0... a real mixed blessing. Some people love 
this idea, some people hate it. Static imports exist only to save 
you some typing. If you hate to type, you might just like this 
feature. The downside to static imports is that - if you’re not 
careful - using them can make your code a lot harder to read. 
The basic idea is that whenever you’re using a static class, a 
static variable, or an enum (more on those later), you can 
import them, and save yourself some typing.
Some old-fashioned code:
import java.lang.Math;
class NoStatic {
public static void main(String [] args) {
System
.out.println(“sqrt “ + 
Math
.sqrt(2.0));
System
.out.println(“tan “ + 
Math
.tan(60));
}
}
Same code, with static imports:
import static java.lang.System.out;
import static java.lang.Math.*;
class WithStatic {
public static void main(String [] args) {
out
.println(“sqrt “ + 
sqrt
(2.0));
out
.println(“tan “ + 
tan
(60));
}
}
ß
If you’re only going to use a static member 
a few times, we think you should avoid 
static imports, to help keep the code more 
readable.
ß
If you’re going to use a static member a lot, 
(like doing lots of Math calculations), then 
it’s probably OK to use the static import.
ß
Notice that you can use wildcards (.*), in 
your static import declaration.
ß
A big issue with static imports is that it’s 
not too hard to create naming conflictsFor 
example, if you have two different classes 
with an “add()” method, how will you and 
the compiler know which one to use?

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   287   288   289   290   291   292   293   294   ...   555




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