You are on page 1of 2

|

|Om Shr
iSai|
|

StaticMember s
Whenacl assi sdef ined, t
hedat amember
element sar eusual lydef inedi nt hepr iv atesect ionoft he
class, soast opr ot ectt hem f rom anynonsuppor ted
oper ationi nt hemai n()pr ogr am oranyext ernalf unct i
on.
Whenobj ect sofsuchacl assar ecr eat ed, eachobj ectwi l
l
cont ainacopyoft hedat amember si nt hepr iv
at esect i
on.
Thesemember sar et ermedasobj ectv ariablesast heyar e
reflect edi nev eryobj ectt hati scr eat ed.
Thedat amember sofacl asscanal sobedef i
nedas
stat ic.Thi scanbedonebypr efixingt hekey wor dst ati
ci n
frontoft he v ariabl e decl aration i nt he cl ass def i
nit
ion.
Secondl y,i tisal sodef inedonceoutoft hecl asswi tht he
followi ngsy nt ax:
dat at ypenameof cl
ass: :stat icmember ;
When a member i s def ined as st atic,i ti s
consi der edasacl assv ariabl einst eadofanobj ectv ari
abl e.
i
.e.I rrespect iveoft henumberofobj ect scr eat edf ort he
class,t her ei s onl y a si ngle copy ofa st at i
c member
av ailabl ei nt heent irepr ogr am.
Thi s i s par ti
cular ly i mpor tant when common
i
nf or mat i
oni st obeshar edbet weendi fferentobj ect soft he
samecl ass.Nonst at i
ci nfor mat ioncannotbeshar edbyt he
obj ect s.St atici nfor mat ioni sst or edatacommonpl acef or
allt heobj ect st oaccessi t.St at i
cmember sar enotl i
mi ted
toanypar t i
cul arobj ectbuti sacommonmemberf ort he
ent irecl ass.I tcanbeusedt okeepat rackoft henumberof
obj ect scr eat edf orapar ticularcl ass.
Li ke st at ic dat a member s,memberf unct ions ofa
class can al so be def i
ned as st atic by pr ecedi ng t he
funct i
ondef initionwi tht hekey wor dst at i
c.Ast aticf uncti
on
i
sal soconsi der edapar tofacl assandi snotl imi tedt oany
object of t
he cl ass. A STATIC MEMBER FUNCTI ON
CANNOTREFERANYNON STATI CDATAOFTHECLASS,
BUT CAN REFER ONLY THE STATI C MEMBERS OFTHE
CLASS.Ast hestaticmemberf unct
ioni snotapartofany
parti
cul
arobject,unlikeanor malmemberf unct
ion itis
nevercall
edw.r.t
.anyobj ectoft
hecl ass.I
tiscal
ledw. r
.t
.
the name oft he class to whi
ch the memberf unct
ion
bel
ongs.

obj
.f
n();nonst
ati
cmemberf
uncti
on
cl
assname:
:f
n()
; stat
icmemberfunct
ion

You might also like