Head First Java a brain-Friendly Guide Kathy Sierra and Bert Bates



Download 17,18 Mb.
Pdf ko'rish
bet63/90
Sana22.06.2022
Hajmi17,18 Mb.
#693704
1   ...   59   60   61   62   63   64   65   66   ...   90
Bog'liq
(Head Up) Kathy Sierra Bert Bates - Head First Java, 3rd Edition Early Release-O\'Reilly Media, Inc. (2021)

int size = 420;
String name = “Donny”;
But when you don’t initialize an instance variable, what happens when you
call a getter method? In other words, what is the 
value
of an instance
variable 
before
you initialize it?


Instance variables always get a default value. If you don’t explicitly
assign a value to an instance variable, or you don’t call a setter method,
the instance variable still has a value!
integers
0
floating points
0.0
booleans
false
references
null
The difference between instance and local
variables
Instance
variables are declared inside a class but not within a method.
class Horse { 
private double 
height
= 15.2; 
private String 
breed

// more code... 
}
NOTE
Local variables do NOT get a default value! The compiler complains if you try to
use a local variable before the variable is initialized.
Local
variables are declared within a method.
class AddThing { 
int a; 
int b = 12; 


public int add() { 
int 
total
= a + b; 
return total; 

}
Local
variables MUST be initialized before use!
There are no Dumb Questions
Q: What about method parameters? How do the rules about local
variables apply to them?
A:
Method parameters are virtually the same as local variables—they’re
declared 
inside
the method (well, technically they’re declared in the
argument list
of the method rather than within the 
body
of the method, but
they’re still local variables as opposed to instance variables). But method
parameters will never be uninitialized, so you’ll never get a compiler error
telling you that a parameter variable might not have been initialized.


But that’s because the compiler will give you an error if you try to invoke a
method without sending arguments that the method needs. So parameters
are ALWAYS initialized, because the compiler guarantees that methods are
always called with arguments that match the parameters declared for the
method, and the arguments are assigned (automatically) to the parameters.

Download 17,18 Mb.

Do'stlaringiz bilan baham:
1   ...   59   60   61   62   63   64   65   66   ...   90




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