— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

class Player {
static int playerCount = 0;
private String name;
public Player(String n) {
name = n;
playerCount++;
}
}
public class PlayerTestDrive {
public static void main(String[] args) {
System.out.println(Player.playerCount);
Player one = new Player(“Tiger Woods”);
System.out.println(Player.playerCount);
}
}
Static variables are initialized when the class is loaded. If you 
don’t explicitly initialize a static variable (by assigning it a 
value at the time you declare it), it gets a default value, so int 
variables are initialized to zero, which means we didn’t need 
to explicitly say “playerCount = 0”. Declaring, but not initial-
izing, a static variable means the static variable will get the de-
fault value for that variable type, in exactly the same way that 
instance variables are given default values when declared.
All static variables 
in a class are 
initialized 
before 
any object of 
that class can be 
created.
The playerCount is initialized when the class is loaded. 
We explicitly initialized it to 0, but we don’t need 
to since 0 is the default value for ints. Static vari-
ables get default values just like instance variables.
Default values for declared but uninitialized 
static and instance variables are the same:
primitive integers (long, short, etc.): 0
primitive floating points (float, double): 0.0
boolean: false
object references: null
File Edit Window Help What?
% java PlayerTestDrive
0
1
before any instances are made
after an object is created
Access a static variable just like a static 
method—with the class name.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   265   266   267   268   269   270   271   272   ...   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