— Scott McNealy, Sun Microsystems Chairman, President and ceo


a constructor if the compiler



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

a constructor if the compiler 
writes one for you?
A: 
If you need code to help 
initialize your object and get 
it ready for use, you’ll have to 
write your own constructor. You 
might, for example, be depen-
dent on input from the user 
before you can finish making 
the object ready. There’s another 
reason you might have to write 
a constructor, even if you don’t 
need any constructor code 
yourself. It has to do with your 
superclass constructor, and we’ll 
talk about that in a few minutes.
Q:
How can you tell a con-
structor from a method? Can 
you also have a method that’s 
the same name as the class?
A: 
Java lets you declare a 
method with the same name as 
your class. That doesn’t make it 
a constructor, though. The thing 
that separates a method from a 
constructor is the return type. 
Methods must have a return 
type, but constructors cannot 
have a return type.
Q:
Are constructors inher-
ited? If you don’t provide a 
constructor but your superclass 
does, do you get the superclass 
constructor instead of the 
default?
A: 
Nope. Constructors are 
not inherited. We’ll look at that in 
just a few pages.
*Instance variables do have a default value. 0 or 
0.0 for numeric primitives, false for booleans, and 
null for references.


244
 
chapter 9
Using the constructor to initialize 
important Duck state*
If an object shouldn’t be used until one or 
more parts of its state (instance variables) have 
been initialized, don’t let anyone get ahold of 
a Duck object until you’re fi nished initializing! 
It’s usually way too risky to let someone make—
and get a reference to—a new Duck object that 
isn’t quite ready for use until that someone turns 
around and calls the setSize() method. How will 
the Duck-user even know that he’s required to call 
the setter method after making the new Duck?
The best place to put initialization code is in the 
constructor. And all you need to do is make a 
constructor with arguments.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   232   233   234   235   236   237   238   239   ...   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