— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet233/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   229   230   231   232   233   234   235   236   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

Duck myDuck
 
=
 
new Duck();
Duck myDuck 
= new Duck();
2
Link the object and 
the reference
3
Duck reference
myDuck
Duck object
Make a new reference 
variable of a class or 
interface type.
A miracle 
occurs here.
Assign the new 
object to the 
reference.


constructors and 
gc
you are here
4
241
Are we calling a method named Duck()? 
Because it sure 
looks like it.
A constructor does look and feel a lot like a method, but it’s not 
a method. It’s got the code that runs when you say 
new
. In other 
words, the code that runs when you instantiate an object.
The only way to invoke a constructor is with the keyword 
new 
followed by the class name. The JVM finds that class and invokes 
the constructor in that class. (OK, technically this isn’t the only 
way to invoke a constructor. But it’s the only way to do it from 
outside a constructor. You can call a constructor from within 
another constructor, with restrictions, but we’ll get into all that 
later in the chapter.)
Duck myDuck =
 new Duck();
It looks like we’re calling 
a method named Duck(), 
because of the parentheses.
No. 
We’re calling the Duck 
constructor.
But where is the constructor? 
If we didn’t write it, who did?
You can write a constructor for your class (we’re about to do 
that), but if you don’t, 
the compiler writes one for you! 
Here’s what the compiler’s default constructor looks like:
public Duck() {

}
Where’s the return type? 
If this were a method
you’d need a return type 
between “public” and 
“Duck()”.
Notice something missing? How is this 
different from a method?

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   229   230   231   232   233   234   235   236   ...   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