— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

282
 
chapter 10
static final variables are constants
A variable marked 
final
means that—once initialized—it can 
never change. In other words, the value of the static final variable 
will stay the same as long as the class is loaded. Look up Math.PI 
in the API, and you’ll find:
public static final double PI = 3.141592653589793;
The variable is marked 
public 
so that any code can access it. 
The variable is marked 
static 
so that you don’t need an 
instance of class Math (which, remember, you’re not allowed to 
create). 
The variable is marked 
final 
because PI doesn’t change (as far as 
Java is concerned). 
There is no other way to designate a variable as a constant, but 
there is a naming convention that helps you to recognize one. 
Constant variable names should be in all caps!
Initialize a 
final static variable:
At the time you declare it:
1
public class Foo {
public static final int FOO_X = 25;
}
In a static initializer:
2
public class Bar {
public static final double BAR_SIGN;
static {
BAR_SIGN = (double) Math.random();
}
}
OR

static initializer
 is a block 
of code that runs when a 
class is loaded, before any 
other code can use the 
class, so it’s a great place 
to initialize a static final 
variable.
class Foo {
final static int x;
static {
x = 42;
}
}
this code runs as soon as the class 
is loaded, before any static method 
is called and even before any static 
variable can be used.
notice the naming convention -- static 
final variables are constants, so the 
name should be all uppercase, with an 
underscore separating the words
File Edit Window Help Jack-in

Download 40,57 Mb.

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