— Scott McNealy, Sun Microsystems Chairman, President and ceo


I could swear I’ve seen code that



Download 40,57 Mb.
Pdf ko'rish
bet266/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   262   263   264   265   266   267   268   269   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

I could swear I’ve seen code that 
calls a static method using a reference 
variable instead of the class name.
A: 
You can do that, but as your mother 
always told you, “Just because it’s legal 
doesn’t mean it’s good.” Although it works
to call a static method using any instance 
of the class, it makes for misleading (less-
readable) code. You can say,
Duck d = new Duck();
String[] s = {};
d.main(s);
This code is legal, but the compiler just 
resolves it back to the real class anyway 
(“OK, is of type Duck, and main() is static, 
so I’ll call the static main() in class Duck”). 
In other words, using to invoke main() 
doesn’t imply that main() will have any 
special knowledge of the object that is 
referencing. It’s just an alternate way to 
invoke a static method, but the method is 
still static!
DateForma
t.getDate
TimeInsta
nce();
DateForma
t.getTime
Instance(
);
NumberFor
mat.getPe
rcentInst
ance();
Make it Stick
Roses are r
ed, 
and known
 to bloom l
ate
Statics can
’t see 
instance v
ariable sta
te


numbers 
and
statics
you are here
4
279
Each Duck object has its own 
size variable, but there’s only 
one copy of the duckCount 
variable—the one in the class.
Static variable:
value is the same for ALL 
instances of the class
Imagine you wanted to count how many Duck 
instances are being created while your program is 
running. How would you do it? Maybe an instance 
variable that you increment in the constructor?

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   262   263   264   265   266   267   268   269   ...   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