7Java this Keywo-WPS Office
7Java this Keywo-WPS Office
athi
sKey
wor
d
I
nthisar
ti
cle,
wewi
l
llear
naboutt
hiskey
wor
dinJav
a,howandwher
etouset
hem wi
tht
hehel
p
ofexampl
es.
t
hisKey
wor
d
InJav
a, t
hiskey
wor
disusedt
oref
ert
othecur
rentobj
ecti
nsi
deamet
hodoraconst
ruct
or.For
example,
cl
assMai
n{
i
nti
nst
Var
;
Mai
n(i
nti
nst
Var
){
t
his.
inst
Var=i
nst
Var
;
Sy
stem.
out
.pr
int
ln(
"t
hisr
efer
ence="+t
his)
;
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Mai
nobj
=newMai
n(8)
;
Sy
stem.
out
.pr
int
ln(
"obj
ectr
efer
ence="+obj
);
RunCode
Out
put
:
t
hisr
efer
ence=Mai
n@23f
c625e
obj
ectr
efer
ence=Mai
n@23f
c625e
I
ntheabov
eexample,
wecreat
edanobjectnamedobjoft
hecl
assMai
n.Wet
henpr
intt
he
r
efer
encet
otheobj
ectobj
andthi
skey
wor dofthecl
ass.
Her
e,wecanseethatt
herefer
enceofbot
hobj
andt
hisi
sthesame.I
tmeanst
hisi
snot
hingbut
t
heref
erencet
othecurr
entobject
.
Useoft
hisKey
wor
d
Ther
ear
evar
ioussi
tuat
ionswher
ethi
skey
wor
discommonl
yused.
Usi
ngt
hisf
orAmbi
gui
tyVar
iabl
eNames
InJava,
iti
snotal
l
owedt odecl
aret
woormorevari
abl
eshavingt
hesamenamei
nsideascope
(cl
assscopeormethodscope)
.Howev
er,
inst
ancevar
iabl
esandpar
amet
ersmayhavethe
samename.Forexample,
cl
assMy
Class{
/
/inst
ancev
ari
abl
e
i
ntage;
/
/par
amet
er
My
Class(
intage)
{
age=age;
I
ntheabovepr
ogram,thei
nstancev
ari
abl
eandt
heparamet
erhav
ethesamename:
age.Her
e,
t
heJavacompil
erisconf
usedduetonameambi
gui
ty.
I
nsuchasi
tuat
ion,
weuset
hiskey
wor
d.Forexampl
e,
Fi
rst
,let
'sseeanexampl
ewi
thoutusi
ngt
hiskey
wor
d:
cl
assMai
n{
i
ntage;
Mai
n(i
ntage)
{
age=age;
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Mai
nobj
=newMai
n(8)
;
Sy
stem.
out
.pr
int
ln(
"obj
.age="+obj
.
age)
;
RunCode
Out
put
:
obj
.age=0
I
ntheaboveexampl
e,wehavepassed8asav aluetotheconst
ruct
or.Howev
er,wear
egett
ing
0asanoutput
.Thi
sisbecauset
heJavacompilergetsconf
usedbecauseoftheambi
gui
tyi
n
namesbet
weeninst
ancethevar
iabl
eandthepar ameter
.
Now,
let
'sr
ewr
it
etheabov
ecodeusi
ngt
hiskey
wor
d.
cl
assMai
n{
i
ntage;
Mai
n(i
ntage)
{
t
his.
age=age;
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Mai
nobj
=newMai
n(8)
;
Sy
stem.
out
.pr
int
ln(
"obj
.age="+obj
.
age)
;
RunCode
Out
put
:
obj
.age=8
Now,wearegetti
ngt heexpectedout
put.I
tisbecausewhentheconst
ruct
oriscal
led,
thi
s
i
nsidet
heconstr
uct ori
sreplacedbytheobj
ectobjthathascal
l
edtheconstr
uctor
.Hencethe
agevar
iabl
eisassignedvalue8.
Also,
ift
henameoft
heparameterandinst
ancev
ari
abl
eisdi
ff
erent
,thecompi
l
eraut
omat
ical
l
y
appendsthi
skey
wor
d.Forexample,
thecode:
cl
assMai
n{
i
ntage;
Mai
n(i
nti
){
age=i
;
i
sequi
val
entt
o:
cl
assMai
n{
i
ntage;
Mai
n(i
nti
){
t
his.
age=i
;
t
hiswi
thGet
ter
sandSet
ter
s
Anot
hercommonuseoft
hiskey
wor
disi
nset
ter
sandget
ter
smet
hodsofacl
ass.Forexampl
e:
cl
assMai
n{
St
ri
ngname;
/
/set
termet
hod
v
oidset
Name(St
ri
ngname){
t
his.
name=name;
/
/get
termet
hod
St
ri
ngget
Name(
){
r
etur
nthi
s.name;
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Mai
nobj
=newMai
n()
;
/
/cal
l
ingt
heset
terandt
heget
termet
hod
obj
.set
Name(
"Toshi
ba"
);
Sy
stem.
out
.pr
int
ln(
"obj
.name:
"+obj
.get
Name(
));
RunCode
Out
put
:
obj
.name:
Toshi
ba
Her
e,wehav
eusedt
hiskey
wor
d:
t
oassi
gnv
aluei
nsi
det
heset
termet
hod
t
oaccessv
aluei
nsi
det
heget
termet
hod
Usi
ngt
hisi
nConst
ruct
orOv
erl
oadi
ng
Whil
ewor kingwit
hconst
ruct
orover
loadi
ng,wemighthavetoinv
okeoneconstructorf
rom
anotherconstr
uct
or.I
nsuchacase,wecannotcal
ltheconst
ruct
orexpl
i
cit
ly.I
nstead,wehav
e
tousethiskeyword.
Her
e,weuseadi
ff
erentf
orm oft
hiskey
wor
d.Thati
s,t
his(
).Let
'st
akeanexampl
e,
cl
assCompl
ex{
pr
ivat
einta,
b;
/
/const
ruct
orwi
th2par
amet
ers
pr
ivat
eCompl
ex(i
nti
,i
ntj
){
t
his.
a=i
;
t
his.
b=j
;
/
/const
ruct
orwi
thsi
ngl
epar
amet
er
pr
ivat
eCompl
ex(
inti
){
/
/inv
okest
heconst
ruct
orwi
th2par
amet
ers
t
his(
i,i
);
/
/const
ruct
orwi
thnopar
amet
er
pr
ivat
eCompl
ex(
){
/
/inv
okest
heconst
ruct
orwi
thsi
ngl
epar
amet
er
t
his(
0);
@Ov
err
ide
publ
i
cSt
ri
ngt
oSt
ri
ng(
){
r
etur
nthi
s.a+"+"+t
his.
b+"
i"
;
}
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
ingobj
ectofCompl
excl
ass
/
/cal
l
stheconst
ruct
orwi
th2par
amet
ers
Compl
exc1=newCompl
ex(
2,3)
;
/
/cal
l
stheconst
ruct
orwi
thasi
ngl
epar
amet
er
Compl
exc2=newCompl
ex(
3);
/
/cal
l
stheconst
ruct
orwi
thnopar
amet
ers
Compl
exc3=newCompl
ex(
);
/
/pr
intobj
ect
s
Sy
stem.
out
.pr
int
ln(
c1)
;
Sy
stem.
out
.pr
int
ln(
c2)
;
Sy
stem.
out
.pr
int
ln(
c3)
;
RunCode
Out
put
:
2+3i
3+3i
0+0i
I
ntheabov
eexampl
e,wehav
eusedt
hiskey
wor
d,
t
ocal
ltheconst
ruct
orCompl
ex(
inti
,i
ntj
)fr
om t
heconst
ruct
orCompl
ex(
inti
)
t
ocal
ltheconst
ruct
orCompl
ex(
inti
)fr
om t
heconst
ruct
orCompl
ex(
)
Not
icet
hel
i
ne,
Sy
stem.
out
.pr
int
ln(
c1)
;
Here,whenweprintt
heobjectc1,
theobj
ecti
sconver
tedint
oastr
ing.I
nt hi
sprocess,
the
toSt
ri
ng()i
scal
led.Si
nceweov err
idet
hetoSt
ri
ng(
)met hodi
nsi
deourclass,wegettheout
put
accordi
ngtot
hatmet hod.
Oneofthehugeadvant
agesofthi
s()i
storeducet
heamountofdupl
i
cat
ecode.Howev
er,
we
shoul
dbealwayscaref
ulwhi
leusi
ngthis(
).
Thisisbecausecalli
ngconstr
uct
orfr
om anot
herconst
ruct
oraddsov
erheadanditi
sasl
ow
process.Anotherhugeadvant
ageofusi
ngthi
s()i
storeducet
heamountofdupli
cat
ecode.
Note:
Invoki
ngoneconst
ruct
orf
rom anot
herconst
ruct
ori
scal
l
edexpl
i
citconst
ruct
or
i
nvocati
on.
Passi
ngt
hisasanAr
gument
Wecanuset
hiskey
wor
dtopasst
hecur
rentobj
ectasanar
gumentt
oamet
hod.Forexampl
e,
cl
assThi
sExampl
e{
/
/decl
arev
ari
abl
es
i
ntx;
i
nty
;
Thi
sExampl
e(i
ntx,
inty
){
/
/assi
gnv
aluesofv
ari
abl
esi
nsi
deconst
ruct
or
t
his.
x=x;
t
his.
y=y
;
/
/val
ueofxandybef
orecal
l
ingadd(
)
Sy
stem.
out
.pr
int
ln(
"Bef
orepassi
ngt
hist
oaddTwo(
)met
hod:
")
;
Sy
stem.
out
.pr
int
ln(
"x="+t
his.
x+"
,y="+t
his.
y);
/
/cal
ltheadd(
)met
hodpassi
ngt
hisasar
gument
add(
thi
s);
/
/val
ueofxandyaf
tercal
l
ingadd(
)
Sy
stem.
out
.pr
int
ln(
"Af
terpassi
ngt
hist
oaddTwo(
)met
hod:
")
;
Sy
stem.
out
.pr
int
ln(
"x="+t
his.
x+"
,y="+t
his.
y);
v
oidadd(
Thi
sExampl
eo)
{
o.
x+=2;
o.
y+=2;
}
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Thi
sExampl
eobj
=newThi
sExampl
e(1,
-2)
;
RunCode
Out
put
:
Bef
orepassi
ngt
hist
oaddTwo(
)met
hod:
x=1,
y=-
2
Af
terpassi
ngt
hist
oaddTwo(
)met
hod:
x=3,
y=0
I
ntheabov
eexampl
e,i
nsi
det
heconst
ruct
orThi
sExampl
e()
,not
icet
hel
i
ne,
add(
thi
s);
Here,wearecal
li
ngt headd(
)methodbypassi
ngthi
sasanargument.Si
ncethi
skeywor
d
contai
nstheref
erencetotheobj
ectobj
ofthecl
ass,wecanchanget
hev al
ueofxandyinsi
de
theadd()met
hod.