Professional Documents
Culture Documents
IMG - 20221214 - 143924 (20 Files Merged)
IMG - 20221214 - 143924 (20 Files Merged)
. 0
TOPIC 1 AJE PAGE NO. SI G .
I Company Profile 7
2 lmroduction To Ja\'a 8
4 lnstalL11ion Of Ja\'a 10
s Configuring Variable 11
7 Package 15
9 lnheritnncc 17
10 Variable And Method 18
12 Interface 22
14 IDE 27
15 History of Java 40
16 Fcaturcs ofJava 43
0132csl 11116 I
Introduction to Java
Wih d1C nwrton of nlieftlf>rootS."'11>. 111" \\'Or+.! " >C1C1 mly dc"'bped "ill .op!U,1.,,,1ed
C<l1J4'111Cr<S. sysiem;, ond dc\iccs. Maoprocessors arc l6Cd 11 COfl'l>lltt<. ielevisons. aid
lilX n<1<1w1C., f\'Cn lhc hlnd·held dc\1e"' ..ch a, P"l"O.. PDA> (P ml DigJlal As;.1>tn1•
ord eel phones nnc use of rric""'""1CSSOl'S. Al lhese clocll'Onl< dC\'ilcs are he"ful because of
l.h.!r eo1nTU11Catl()n capabific:-.. \Vllh 1h.! •'CTC"asilw cupabilil onJ dai.... c0$1I or
11bnro1ion PIOCCSS"1! and 11C<\\'Or1<"11 1ecmobgies. lhc ne1wo11' Ii grow1111
rapid!) !Or
lm®nlllina idomuiion 1hroui;l1 tl<c1ronie S)'il<""-
,. rturonn ind<ptndtntt • Many i:uvnll'S are OO"l"'•l>le "'ih oriy one pl:ui>nn Ja'" Wlb
"11CCdicoll) d<>.,..,.:I so 1h11 • \\'Oiild run on any conlltlcr. rcprdl..-S> 1r • was nnwng
Wildo""·LnL-. Mac. Unix orany of the other open lclllii.
, Sln.,tr and t•>)' 10 ust .. J:&\7t'S crcal 1R.".<1 10 dcsigJ1 IM> code couU be \\T'lllcn
effic:1011ly nrd ca<il).
,. M ultl fllDCllonal • Ja\'a can produce m'II) n1>plic•t1i<>1" from co1motd·l'1c program• to
tipplels tO Swi1! windo\\'S (lmsa:aly. sopliiitca1ed graphical user ).
'""' does l<we son"' dr.1., ks. Sitt I hos •l.IOlmtcd i;uboge rol'.CllOfl. I can IO
n'Orc "'""''Y dnn Olher 8C$- There arc often 111lk'.1n'l.'lllt1011 ddi'.l'CllCC$
use
difl'.1C111 pb1i>rm<. "hic!i "'" led 10'"'"
unibr on btirl! de<ert>cd as a '\\'rile once. IC'I C\'Cl')"'tierc•
S)"lcrn. l..Mlly. U1CC l """ an ob,1ract '\'W1WI n'Dl:hir". a i;tr"'"" Ja\:1 P"'ll"""' 00.,,.n "'"" ocecss
to lhc l'atr.'< APl's on a tern dircc1ly. 1'onc of these i;suos arc mml but I can 111ean
th:tt J:1\'3 tin•t an ap1>ropri:•tc chocc.- IOr a utic:ubr pteee o( !!o<>h'3.
0132csll 1116 I
The Java Platform
One thina ""' distioJishcd JB\11 from som: other bro<t is as abilily to rm 1hc "'""'
ca1rpilcd code across "" le opcllllr@ · .In odn t•l!C>·thc source code (oo<k: 11101 is
wmen by 1he prol!"'n'll'CI'). is corrpilccl by a con"llilor inlO an c"<Ccwblc file. This file is in
m:.cbn: t111g11:•ll"·and is ncndcd IOr a sq;Jc opcrn•il! S) tcm'procl'SSOr co.mimw1\ so
d'C pmlll""mll.T would tu": IO re corrpilc the progrnm s<pcm icly fcir Cllch '"" opcmlif.:
sy.lcm proocs.or co.mna1ion.Ja•11 is dd!Cre,. in tha1 • docs not con..,ilc the code drccdy i110
m:ichn: Dfll:ltll): code. Co1rvl11im creates b)1t-.codc oll of lhc liOU!tC code.
13)1'Xodc lli'n.:rally boks SOn'Cthmg tile thti:
a7 73 Sa lb 92 7d
When ""' code is l\lrl by the user. il ., proc<:ssed by SOt1'Cthil!
coled the Jr"" Vinull Mxhile
(JVM). Tho J1is cs<.emolly on inlcrpre1cr IOr the bytecode h
llJlC< •"""'111•1he b)1eoo<k: nnd runs a. There arc dili:re1u
versions of lhc J VM d<1t are con..,ati>lc "'th each OS and <:4n rm
""' smn:code. 11...,., is \Ttl<lly no difli!rcncc br the end-user. b111
th5 nukes ii 11 lot easier li>r
PN1Pn1n..... .s doing soft\\'firt dC\'Ck> J>11\:'nl.
•• Check installation
, Ja'" •¥ a"'*ly be n.111'CI. on >"" 1111<hno You can icq lho. by Clpl!
l•C 1console
c.r you .... usre Wnlo\\ : 'n+R. ...._.,. cnu and press Ener)
,If •ndn.1.al.'<l.
Ja\11 " com'.Clly ))' '>1"'1:)W
., lhe
'11c>uld 'ICC .Wnnr.ll)ll ...,.. )Ola' Ja\ll
n<tl buon. If lhe'"Icommndoonm•ndinc rellrn< lhe ilbmtuon dut lhe protpm coulJ mt be
,P,\2 •\'ttilOO
'-J.)\lU'""' IO n.iall J.l\11.
"'°"
fil on
e\VindO\\S: If)W are ....... IYW!do\\>. \lrc>I)' nm lh< e>c<:wbl: file at>I •JDK lhe
nst.lll1M n>in.oon.
Unix, Solaris. or Unux: tor I.no< and UM. downblcl die 'ldl-1 6.0- &Ir ln1t
' " Sa,.c die downk•kcl lilc ., any dmc.Once )W """s;i\"C:d die lie. c'111ICI I IO
a P*" lh.11 )W CUI ranmbcr. by u;q; Tcrmn:il or b) clolble clr:lrc on die lie.
Wh-.n )'OU ID\'C li-..hcd CWll<"1! lh< lilt.copy die JDK I60 i>U<r anl Jl"Sli: I ., lhc
wloca•Jo paS1C IO die wbcal dRCIOI)'.)W"' 'IO be ".-) so ""' C\a)" ll!Cf can
""' lhe JI'" h You •.,.dd:.tc lh< do'A1*>1tk'CI fil: \0 11'11 I doan"I !ale >I"""'
on )Ola' dmc.
la cf ntosh: The: b1oi G\llllble JDK " a..01miol)' l\lia.. ..i die opcn11rc
by
•)"Siem llccousc J.,,.
i>r l\bcirn.h " dl:'Cbpcd and nmunod by Apple. n
coonlnuion "lh SIA die Nl'CrC 'mon on lhc \11enosl1 RI>) not be die ,.
,..,... ,.""' ""'"ibblc
ote on Editions
, 1'hcJDKc:on>OSn
drcccd1<>11L
OIJ?csl 11116 I
,. Ja•-.Slllndnrd l:dllon (JSE) nus \'CrSIOO " lhe base pblilm1 i>r Jn\'a. The course
wl k on t:d•1rJ1\.
,. Ja•-a
d1>1ri>Ulcd nd•o;.>r
l:.nlctpl'l>C (JI:.!!)
l:.d•on 1rch11«Hrc Tho.
Juv.acdom " 1ro11ly
•ppbltJ<b. b;i '"bq,\cly on and """
dc\1:"""'1g
co1T110nct1S Nri1G on on apploc:uion .mer.We wiD no1 be
°""'"'11: rro .ot"" •m.IOn 11
COll'>C,
,. J.,., Mrro Ed•on (JME) Tho. cdtlOll " p"1mily i>r d!:''Ck>pl\J d.., "'
Pl'Ol:l'l'"'
1hc
10 run on
CO!Nll\."r applt.:aroce>. ,11;h 11'PDA> and cd
phone< . .
Configu ring Va riables
,. llcM .,,.,. .code. • "' reco1111\....Jcd d<lt )<Kl Sci 11., l'illh •-ari;1ble on )<lur •>•cm
so)<Kl can con'!Jile yow code n'IOrc easily.
,. from lhc Co1irolPoncl do<ble clo;:L "S)'icm" (Sy..1cm and Maiurooec 11 V1>111)
,. For Window< 7 or Vi.tu. click on "S) lcn\• "Ad'1111Ccd S) leln Sc11• on Ille let. and
11""1 on"l:rnironnli!n l Vlll'Ublcs.•
,. For XP and 2000.ck on lhe "Ad•-.nccd" lllb and ck on ilm'l'\lrn>\.'l"t Varobl...- " for
NT.clr.:k on lhc •mironmc1•" iab.
,. Scl'CI 1hc Pulh \"Jmblc ond dkL i:.d•"
,. Adel ie p:uh IO ie b11 drcclO<)' of "lli!rc Ja\11 1> lSlal'd on )our hard dl1\"- II
probably be C"'ro•m f'bVu•-a )<IL I 6.0 20.bin
shoukl 11llcs$ )<Kl cl\s'll duri1J
•
in>talb1ion.
,. Ck:L OK
•:• For lifllL't: afld UNIX
,. Ore "'flY IO >Ct )<lllr pa1h 11 Lnllt!Un,. !;IO add a p.ilh cxpon w yow bash profile.
,. In order IO do lhl>. In! open yow b:t>h profile in a ICXI cdor For c'<lln'!Jlc.
,. pko -1.baiJl_profile
,. ,,,..,. udd lhi< line:
,. e>.pe>n PA111 SPATI!iusr locol Jdklbm
,. Note dot dc palh IO 11-c ju\-a dio'CCl<liy •l"r'\icll ,.ik>n" 1my be ddlc
on )'Our
nnchl C.
,. Rc<iart your •l-cl
0132csll I 116 j
•:• For Muci11tosh
;. Apple sets c•ihing 14) br you. Si ba.ck ard rebx.
,. "fbo only dnl\\back ti tlklt bl'C3U.C j\ppl: ltuidb dC\\!kJt)nr1• und nnl1le1'111CC
JJl\'a !here ti usualy n dcby tom lhc 1ire tl'<lt n n-.w'""'"" ;,, relcose<I by Sun
of M3c,
on lhc
111.t the tin.:th:it the new '""' ii rcl!ase<l on dlC Moc. Also. l)'lli1g dic btcst \
som:to1...s "'<l""''S an op'.r.lliJG sy>1cm
cnion up!
lr'ldc. °"
,. Oh \W ll )\lll can't "'"c\\!ry1lung.
Va lidate installation
, S\\"lch a,pil 10 the co1l1tnnd line :u n11 lhe tOl>\\'ing co1muncL
,. ja'ta ..,\:!Oil
', The ou1pu1 should be sin br to lhc blowing m•pUL
,. ja,..'""'"'" "I.7.0_2S"
,. OpcnJDK R'""''"' EnvironO'lcnt (lccdTeo 2.3.10)(7u2S-2.3.IO- I ublln .13.04.2
,. OpcnJDK 64-81Scn<er VM (bu&! 2J.7-b01. nix.:d n'Odc) )
Garbage collector
,. The JVM mmormti.'.aly n: col.".C<S lhe m::mory \\loch " not n:lcrred 10 by other obje<ts.
'Ille jaw g11rl>uge roll< ·tor check• al object rel'n:nccs and Md tt-c objects \\hich can be
11U1<> 1mlL'lllly rel.!a>cd.
,. While 1hc tba-c collector rel-a<es tho: prosromm:r li'om lhe rlCCd 10 cxplicaly
nnnajje n>.'.lllJI)' dli! progr>1T111i!r still need lO ensure th:il he d0<-s not <'<P
1111-..,.,c'<l objccl rel'n:.-.'.C'S otherwise the i;:1tbai;c col>. .:tor C011110t relc:be the
associaled 111Cn10ty. K"'Pi11! lllllCCdcd obJCCl rctcrenccs arc lypacnlly cl>'.<l memo"'
leak;.
0132csl11116 I
Classpath
,. 11'c clt1t</><llh dcfin;:.. whore th!Ja•-a con-..il<rond JO\-. n1ii110 look !Or •o
fil.'< to
looo.'111t. ln<ll\ICtlon can be t-i 11 d"C Jn"• progninl
,. For cxa"1'1c { you \\'ll!U to '"" An «tcm>I J11 2 lihllll)' )UU hil\'c to add tho< lhr.vy to
)'OW' cll>spath to ""' 101)Uur prol!J'llm
HelloW01Id x
II A Sftall Jav1 progr.,. publlc cl••
lloworld (
publtc 1t1ttc vold ftaln(Strlng[)args)(
Systeft .out.prlntln'(twllo Wor
ld.);
}
c• TabWldtl L.116,Col
5.,.
INS
8• Z
1hc '°urec code i1 )Ullr ja,·atllr din'.CIOry \\..h the H• l w d.1•v• &:n,u .Tiic nan-r
or• J"'"so file nut ah'"'Y" e<11.11h rhc ck1ss n:1n"C (withn 1b: "°'"'° t-.odc) nnd end,
\Y1.h the .ja • C'\lC'lbion. In this C\lln1)1c lhc fir1u1 n•bl be tt lo" r l<i. av•
11'u. usc d'C cb's ;, call::d 11 •or ..
0132csl 11116 l
,. 01>tn u she ll for oornmand line ICetSS. S"'llCh 10 the }t11 adir dn".CtOI)'
1>ih'<ltnmnd
the cd j11•..dir. IOr ""'"'111<
i1 tho obo•...- c'1l1111lc
vii t he e
conmmd. Use the 1 co111mrd (dlr under M i.-.rosoft
\ h "'•\• qella\ J•• •t•rtor
\Vtndow,) lO \'Crify lhJ1 the .om:c filo b 11 1hc diro<iory.Con.,il: ymr Jo•11 .ourcc
Iii!
u-.o acbss lilc wilh the li>lklwing comnund.
djava c HeDo\Vorld.ja va
conmino now o &le 1lclb\Vo1'kl.cbss". I f )\Ju ...c tho; file you
rccwry fully COfl'1il-d
SUC«S<. h:m: yutr fl'SI Jll\11 SOll'CC code into
t\ffcrwnrds !tit uin lhe eo1•cn1 of tile dn'<'IOI)' wilt t he II or dlr cormnrd 11v:
b)1ccOOO.
You con OO\\' stan )1011r 00111lib:I Jtt\71 !Ptm F.nstR 1h:lt you 11re stl i1 the •rdir
di'CC!ory ond mer the folbwing oonm111d to >1un yotr J11•11 proi;rani
j!lV!l Hcl!oWorld
java HclloWorld
If yuu ore oot in the di'CCtory i1 "ilich the compil..-d class is <torcd tlv:n the S) cm
jon.li1n;:.N
rc oCbu
ult)'J'COtfFnund Error: rtsllff
shoukl
,. To usc the class
utCl•ss "nmi11 11
the Ml cm>r
i>lbwa1g 1ocss.:1.,:
con1m1•I. RcpbccExcept.ion In
"mydi'cclOry" "•h the
lhreod
dr"."Ctory
wheh corinils the lcsl dR.-clory. You <houkl oi;iin $'"lhc ,clloWorU " 01nplA.
0132cs 111116
l
ja,•a -classpat h "mydirectory" H ello\Vorid
•• Package
•• Class
O.f..T<111platr 1hu1<i<'l<nbtl tlrt• d<1tuunJIH-lr<l•·ior<mtx1<11t•l >111/r un
1mtun<efl{t/1J /
cloi
In JO\'I <Cl\ICC code •class "clcliicd by lhc f1<1n keyword and nu.I>tan "rrh 11 capal
bicr. 11ic body ora ells> "swrounlcd by 11.
O IJ2csl II ll6 (
package- te-3t;
lb:dalll &>SOCistcd wih achs.s i.stored in •·ariuhh:s :die lxhol'or IS>()Cillcd toa elm
or object is in1ikm:nu:d wib mnhods.
A cbss is coccmcd ina Jo"' scucc Ii: wib Ur s:mr mm: as th: cbss pbs th:.j:l\11
cmn<lm.
·:• Object
Def.:A11 object a w1 u1stantt of a cl=
1b:objeCI is the real e,klrera wbrh lus <bu ord can pai>mi a<:llons. Each obp:1 as
ere:iu:d based on the cbssdcliniion.
0132csl 11116 I
•· I nhcritance
ACDS> canbedctn'Cd &omanotherelm. In Lhil case1his cbss i< cab!
•sr1h"1LUS.Ano1hcr
C0111'1lJf1 l>tnw b tJ\at Dt/t1U t'Clt•nd\'llltOlh<•r tlil.'U
lrllcru ncc alb" •ems 10 '"""" d1C bcttl\>Jr and dnia dclhlKll.. of ml>lhcr cb>s.
!he (>Jowrna codes dcnll...n1c, how a d.ss cnn t\ltl' another clN. In Jl\;i 1CDS> con
c"cnd 11m><ir11um orone cllss.
p ka c .vc
claaa Mya.1.Cl11a t
void l'l•llof)f
Sytte&.ot.ptlntln c •aello fro.M)'9e1CC:l•
•> ;
rr. cb_q " "'""' another cl:ID • rilr:nt< lhr: rrcdlll<h fi'om b "41<'r<llss. " \\'lll'b to ch.'111S"
1hcsc n>:lhods 1can ""emJ1hcsc n>:dlods.To O\=dc arn'.lhod )OU ""'thesan>: n>:thod
•Y.1un: 11 1hc .oun:c code of 1hc >1ibcliL"-
To 1'1d.:a1c llO II>: n:odcr oflhc source codetudlhr: Ja,11oon.,ik-.r 1tr:11 ,au hi' lhr:
soO\'l"T1"1ie 1rrethod you canU§C the •o r
n1cr1oon
nnnot1110
r n.
Th win& codedcrmn;lrole< how>"" can O\=dC •n>:ll'<ld fitlm • bss.
e
·ka c .vo.,. la. ava 'lt r .ba
cl••Hf811eCl•aa I
void hel lo( l I
ly1to•.out.pr l•tlnc 11e110 1e<:l11e
t1oe H 11
0132csl 11116 l
E'W)' WJ '1J'1!irrf>h;i!ly xr<;n,jj !lie t•1 .Th: t <!m\!!;- iP.J
m.>.thods r <>'Cf}' J3\'0I ;
objccL
,. •'I•• 1 u all"' iochcc (lhc Cll'rell object ti cqwl 10 01
•• lnstance variable
lnslnnec wroblc " ossoculcd \\ith on insuncc uf tlr cbss (al.o calbl obj«'I).Acc<'SS "'Orks0\'Cf
tlrscobjects.
lnsl!l""' •wb-s can ht\'c any oett<S eo11rol and con be rmrtcd hi or cran;>:fl. c-
•wbl<s rrwiccd as fmJ can nol bec""""' alrr lb5\!11cd IO a ,,,u.,.
•• Local variable
•!• lethods
ou2cs111 1 16 I
A n'IOlhod t> 1block orcodewth p>r.tnltterS Ol1d n rrtlnl '"b:. II can becalled on
the
objcc
publ ic cla11 HtbodExawpl• C
void t•1t er (C1 r l n9 II f
Syat••.out.println(" ello Wo11c J:
Mdhod can be dc.;blt(J w•h ,..r-ari;..In tl1'l ca>C lhc n'IOdlOd dccbrcs 1
\\heh •CttJ•L' &um l'Cl\l IOirony ,...,_, ., (s)TCll.:l)f>C m1l'IO.) A n'IOlhod c•n
paran'IOIL"I'
"'""one "'"·atg; i>ftll'IOICr und lhl'! """'be 1hcbs1pororn:t:r
only nlhc ll'IOdlOd.
O'"""nc ofa S'4'C"'bSS n-cdlOd:/\ n'IOdlOd n'Uil be of d'IC CQC1 S31l'IO rcllnl
po.......,tcr "nd the'""" a...,an.'IU.Al.a lhc rctlnl p;1111n'IOl<:f nu.t be lhc """"'·
Q,.,Qa<J n>'.lhod< An U\'\'.fluaJcJ nx:Uud eo. • n"dW "ch lhc wn-w: .-la:. hue
d&f\! "'P-ll:•"b.. 11lC ""'"" l)'PC c11nnot be U>CJ 1(1O\'Ctiolld o n>"."thod
•• Constructor
" CbSS CIOIU" ron,,,,,,,Or\ 1h:U arc 11'\0J..t". d to C:tt31C ObJCCU on It.: CbSS
dchlon. ColllUUCtor dccbmto"' bol Ile n'IOthod dccbm101" c"'cpt d'll lhcy use tl-c
mn-c of lhc cbss nl ta"'
oo rc11ni l)'PC A cb<s can """ SC\"1111 co1\'iuu::1or11 wih
ddi"l'C111 parall'IOlc Loch cl1ss mJ.\l dcf11C DI b>Ione «ll"1ruCIOr
Inlhc win& C.\Jlll1)lo lhc con.ll\ICIOr o(d'IC class C'P""lS
k• •
poran-clcr
... 1 •
•••1
•v• t' .
St rifMI II
0132csl II116 I
lfno•'Plioll oonslNCtOr "defined lh:COITf!il'.r odds"'l'hi:1tly •oonstn.c10r 1rin:
ells>
.. .....< thon lh: conitt\ICIOf of lh: ...,..cbss " M";I)'"'l'IC1t ly cabl II ti-..
ca>c.
In lh: blowm11 <>Wrc>I:: lh: dclinton or lh: c:ori<tructor ,..,.,.. p;llll rc1m. (:al.o
public
Cl•••M,Con1trctor£x ....,1•
•I •public
ti
M,ConatructorEx&lfPl•t J
' I
t
11\!mltWlg """",..."" brcrcatq acoo"rna:tor "the b""'mg. , .......
..
obp:t •cn-aled ba>Cd ona con;cructor Thi. CCftMlC10r nWlod •lh:
•iatm..,. cal..S bcbrc al1)"1ing elsecan bedone....... the
obJCCI.
Modifiers
.:. Access n1odi liers
11\!rc arc nl>Cla.,.. kc)'words 1\2ilbl:: 11 J0\2 />UNu-. I'"""
"'d and
dYCC an: w accc<s l::wl..
11\!rc pubt.c. protoctnl. J<fo11/1 and pn••ot. l1IC) dc&nc hov. lh:
I'"'or.
c:omspoud..cl::m'.111 a '"l>I:: to other C:Ol1"f!Oncrti. If <01R'.111111 " dcclll'<'d
cb.s.cs or n'Clho<h can be ice!> crca...S or callod by other
publi:.c.a. Ja,,.
objCCU. If son>::dq 11
dccbl'<'d pn12tc, •-II- • n.'lhod. • can only be aa:cssal "-chll the class 11 ..111cfl • "
dcdmd. pn>1oc1n/ llnd dtfuu/1 ore snmr. A pn>trc1"'1 ells> can be ICCCSSCd li'om lh:
pac:k•ac ol\l'lb-cb.<>a o....X the p.'IClaWo' "'*• tl<fuu/1 clL-s can 'Ionly
aa:c<.<cd ..
tllC wn: pac:l.a 11\! blo"111g tllbl:: dcscrl>cs tllC ,..,l>1bty:
01320$111116 l
no y
imdifa N
N N
y
Pnl'OIC
Y
Oth er niodific rs
,hi m:thod> <•mot beO\TClcn 11•abcb»
, abitrllet ircthod: no n-.:1hod body
,. i)11tll'onlll'.d n>:lhod '. lhm11 "'"·can be fml and """ any OCCCS< cor1rol
,. mll\'C rn'.tl-ods: pbti>nn clcpcroa• code.1pply only 10n>:lhods
,"1n:I ebS> Ot' 11\:1hod
ln1por t statcn1cnts
•:• Usagt of import statemen ts
In Jna )OU I"'"" 10 occc.sach» •"-> ,..u W.q.. 1or.. ..i mn>:. e.g. lhc pock•g:
mm: •nl 1hc class nunc Yoo can odJ • • s1.11<11\:fti i>r cbssd or pock"I'$ -
)""ebss Ii:. 1<lta:h 1Do1< )OU 1011'.iC 1h: rcb1cd cbsscs 11 >"" code "11houl 1hc
p'.K'lDJIC qu.1•f1<r
0132C5l ll l 16l
•• I nterfa ce
/111ift1ccs an: co111r.1CIS i>r wlu1 u cbss can do bll lh::y soy oolhing aboll lhc way in
which lhc cbs. nU>I do L An l111t'ef<1ce b a l)l'C s11tibr IO o cb.s. Lilcc a cbss un
if1.4T£tcc dctn:s 1n:l00d:s. An Ucrflcc can ha\"c only obstn1cl 11..::thod.s. no concrccc
nli!thods an: d>wcd. Methods dcli>cd i1 dcreic.,,. an: by dcfaull pubi: "nd ubstrnc1
c : q dcci:1111tion or ''"'"" modifier.. i< op1ional ln1crilcc• can """ consuu.. which
"" an: fn1>1lclly r>ooi:.>1311: and lilll. A cb» c.>n fn1>1cn>!IU 11n iwcrilcc. In
... cusc
d> ITUS1 provDc concrete i11JL-m:!Uatiolti or the OUCriilCC nl:lhods. If you override a
' '" i
rnclhod defiled by un 111cr )'OU con ubo ""' the tovu nd• Ola-tiol\ 1k
k>llo"•ing ca<lc hoY.'S l1n c:<a111J': -.,,1cnl.".1'141tOn of 1111 itt.crflcc arxl il.s
>usap: y.'iJlil 11 cbss.
1 t ne vec• -. h t
vo- 1<.I t•1t C J 1
void wrlte Strlnq e):
I
Class met hods and class va ria bles
Cbss rnclhods and cbss \nmbl'$ '"" assoc:a1cd wilh lhc cbss 11nd no1 an instance
ort i>! dis.. Le. obj<'CIS. To n: '< 10 lhc;c clc11-... ,. )'OU c11n i..c 1hc cl"m"' t111d a
dot (".") IOll>wcd by lhc cbss 11>:1ho<I or chss'"rilblc ron>:. Class mcll'ods 11nd
cb.ss v:u·ilbio;-s ""' d<ebrcd """h 1hc 1tat1c cywonl. Cb>s n>'.lhods arc oM>
cal'.d "'"' m tlt!Xh und cbss vnriabk:s areacal.".d :1101it· ·url<1hfnor11c1tit·fi /1ls.
An Cl([ll11'lc i>r the US. jlC of 0 Sl31ic lielcl i< punt ln ol' lhc IOlowing Sllllcll\!11C
Sy t.e . ut. .p nt. n ( "H l lo Wor d"). •Ut b 8 suitlC lick!. 311 ObJCCI O( type:
0132csl 11116
I
••..istr •m. and )OU cal chc pr n In c nrchod on dU> objcci. If)'OU dclinc a
scmoc
,.,rob\) doc J•"' r1111••"'1 cmTC>m..,,. ••MX•llcs one cb>' \:in.:1bll r 1 cb-> no
nullor
I•"' nlll1)' i>.tanccs (objecb) .,..,... The ot:11oc ""'bl: """ therci>n: be '""'" as " po"'11
\'aroblc. 11ic •• l •·J• ai
""cng codedorro1i:.tra1c< chc "'"llD or
ntr .l
'" • fieUs.
01Jics1 11116 I
Table 2
\\'bal 10 do Ho''todo it
package
test;
Create • rew cbss cal"l<I MyNn.Cla.u. public cl• HyNewCl••
racka9e teat:
Cmttc a new 01 (im:lncc varilbli:)
calctt 1 ar/ of lypc Slrils ii1bc My•ewClass pblic class MyNeWC ass
cbss privet• Str in9 v•rl ;
ubl1e cl ss NewCltts
Create a Cotor ilr )Ollr MyNeVClass ptivate String varl :
rck•9•test ;
public class
MyNewClass
private Strln9 v•rl:
public
yNevClass(Strinq
Create a 11C\Y m:tJxxt cal:d doSo111eTh1i ra 1vorl
J C• parel ;
1g iJ r t •· • p •l ;
cbss whi:h docs not return a,,..., ond
Im00 p;unrrek:rS
1)'0<I'
public class
private Strlnq v•rl z
HyNewClasa
Create a JlC\Y tn.!lhod cab! doSo111eThi11gl il
)'O<I' cbss which docs not return •1'3llc public
KyNewCla•11Strin9
ms I\\'O p:irnnrters. a Du and a
tlnd ra l)varlC • por•l ;
Person t J • •r •l:
public void
doSo.eThlaotl
0132csl 11116 I
\\liar 10 do Ho"'10 do ii
pUOJ lc woid
OSOM"Th lftq2 ( lri t
tlOlll) f
• Perscin
*
I
t•lt:
blic cl••MyewCl•• C
private Stt lrw, varl:
public KyN•vCl••IScrtnt
ra il I
••rl • p111ral:
r t .1•.va r praJ :
public t rit
S011eThl119ltScln9 •
trinq b.Per.on peraon)
return ; y V4
r:. • e "
w J dO ( r e
I
I
test;
l c class MyOtherClasa
Stri n9 .yvalue:
C,...rc a cbss calbl MyOllt<rCla.r> ,.... .,.o
n.wcc ,,.,w,1es. One ,.ii sron:a sure. °""
doq;
public Strl"9
tMyval C I (
!he
i>r return
orher ,.ii .,_a Dog. Cn::ire p:t1<1" :ind $C'tltt
\",llill>les. ayvalt1•1
public wold
setMyval e (Str.ng
yv -e)
0132csl 11116 I
,,
[Whal lo do flow 10 do II ,,
I ubllc void
0Sometht n92 C int •· Pe r1on
pera onJ l
public
KyN•w(lo11Cltrtn9
t'o l l C r hJ•.v.n r I a r•.1
v•r l • p,i.ra l 1
public int
do 0"'4thin?JCSrift a,
St rln9 1> , l'or1ein rertonl (
r•tur n :
Any Vd e
v J d f
r ti! • •• I' •
I I
- ·f ck•9•
•1\.1
public
cl••H)'OtherCl••t
ere.lie Dd1» cull'.tl MyOtl1t•rC'h1S.f "11h St rlnq •yvtt.l ue:
l"O r1St111Cc '11ri:1blcs. 01ic wil s1orc n Sltlij!. l>oq tJoq:
public Strin9
the otr \\ii tore u l)og. CrcJlc Her and qetHyva lu•C l I
scncr IOr llic<e'111ilblcs. r•turn rnyv•l
I
ue1
I publlc:T void
etMyvalueCStrlng•yvalue)
OIJ2csl I I I 16 I