Upload
mee-nu
View
252
Download
0
Embed Size (px)
Citation preview
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 1/26
Pratian Technologies (India) Pvt. Ltd.www.pratian.com
Basic Java
Unit 5 - Packages
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 2/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
Topics
'aming lashes
Packagesreating Packages in Java
ccessing Package em*ers
ccess +peci,iers
'ested Packages
The import statement
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 3/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
class onnection
/
p0*lic void connectTo1racle()
/
22 Logic o, connecting to
1racle 3B
4
4
onnect to
1racle
developed
*! team
'aming lashes
class onnection
/
p0*lic void connectTo+!*ase()
/
22 Logic o, connecting to
+!*ase 3B
4
4
class 3ata*ase6riter
/
p0*lic static void main(+tring7 8 s)
/
onnection connection 9 newonnection():
connection.
connectTo1racle():
4
4
nother Team ; 0ses 0tilit! classes
provided *! *oth teams < =
//Error
onnect to
+!*ase
developed
*! team =
6hat happens when
all the three classes
are integrated <
3ata*ase6riter
is e>ec0ted ?
@ow do we solve
the pro*lem o,
'ame clashes ?
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 4/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
Packages
Package is a collection o, related classes and inter,aces.
Packages are 0sed to avoid naming con,licts.
The! also provide access protection.
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 5/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
reating Packages
hoose an appropriate
name ,or the package.
Use the package
statement at the top o,
every source file that
contains the classes or
inter,aces.
package oracleDB;
class onnection
/
p0*lic void connectTo1racle()
/ 4
AAAA.
4
package cybaseDB;
class onnection
/
p0*lic void connectTo+!*ase()
/ 4
AAAAA..
4
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 6/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
reating Packages
hoose an appropriate
name ,or the package.
Use the package
statement at the top o,
every source file that
contains the classes or
inter,aces.
package oracleDB
class onnection
/
p0*lic void connectTo1racle()
/ 4
AAAA.
4
package cybaseDB:
class onnection
/
p0*lic void connectTo+!*ase()
/ 4
AAAAA..
4
Note
package statement m0st *e the
,irst line in the so0rce ,ile
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 7/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
reating Packages
hoose an appropriate
name ,or the package.
Use the package
statement at the top o,
every source file that
contains the classes or
inter,aces.
package oracleDB
class onnection
/
p0*lic void connectTo1racle()
/ 4
AAAA.
4
package cybaseDB:
class onnection
/
p0*lic void connectTo+!*ase()
/ 4
AAAAA..
4
Note
There can *e onl! one packagestatement in each so0rce ,ile andit applies to all t!pes in the ,ile.
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 8/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
Package is a li*rar! as well
ph!sical ,older with the same name as the package has
to created
ll the .class ,iles *elonging to that package has to *e
incl0ded in that ,older.
oracle3B.onnection s!*ase3B.onnection
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 9/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
lasspath +etting lasspath setting is 0sed *! the JC to locate class ,iles
lass loader looks ,or a class ,ile in the path mentioned and loads the
*!tecodes in memor! The environment varia*le lasspath needs to *e set to the parent
,older o, the package ,older
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 10/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
ccessing package mem*ers
De,erring to the mem*er *! its ,0ll! E0ali,ied name
class or inter,ace *elonging a package needs to *e re,erred toas package name.class name
class 3ata*ase6riter
/
p0*lic static void main(+tring7 8 s)
/
oracleDB.Connection conn 9 new
oracleDB.Connection():
conn. connectTo1racle():
4
4
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 11/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
reating Packages
+0mmariFing steps involved in creating packages
i. Incl0de package statement
ii. reate the corresponding ph!sical ,older str0ct0re
iii. +et the classpath environment varia*le
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 12/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
ccess +peci,iers - Devisited p0*lic
n! class mem*er declared as p0*lic is visi*le (or accessi*le) to
an! class *elonging to an! package
protected n! class mem*er declared as protected is visi*le (or accessi*le)
to all classes in the same package as well as to s0* classes*elonging to other packages
de,a0lt n! class mem*er declared witho0t an! o, the a*ove is visi*le (or
accessi*le) to all classes in the same package onl!.
private n! class mem*er declared as private is visi*le (or accessi*le)
onl! within the same class
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 13/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
Gnowledge heck Incl0de an! class that !o0 have written in a package and 0se the
class in another class.
'ote H To r0n a class that *elongs to a package remem*er to provide
the ,0ll! E0ali,ied class name as arg0ment to the ava command
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 14/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
Package 'aming onvention It is important to ens0re 0niE0eness o, package names
+ince the Internet domain names are 0niE0e ,or acompan! the reversed domain name is 0sed to name apackage. K> com.pratian.avatraining ,or a package avatraining
Package names are written in lowercase to avoid con,lictwith the names o, classes or inter,aces.
Packages in the Java lang0age itsel, *egin with java. or javax.
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 15/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
'ested Packages - K>ample
lass 0stomer *elongs topackage
com.pratian.pb.model
In another class 0stomer31it needs to
*e accessed as
com.pratian.pb.model.Customer
o0ld *e
0m*ersome
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 16/26Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
The import shorthand
The import statement is a convenience s!nta> 0sed to
access classes that *elong to a package
The import statement
makes it easier to
access package
mem*ers
The import statement
m0st appear at the
*eginning onl! ne>t to
the package statement
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 17/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
The import shorthand
The wild card character * is 0sed to speci,!
all classes o, the
package
U it 5 P k
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 18/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
'ested Packages package can *e nested inside another package.
. separated name is 0sed to nest package within packages.
1n compilation o, the a*ove code we wo0ld get a 0itar.class ,ilethis ,ile sho0ld *e incl0ded in a director! instr0ments2western.
H2m!proect2instr0ments2western20itar.class
package instr0ments.western:
p0*lic class 0itar implements
instr0ments.Instr0ment
/
p0*lic void pla!() / 4
4
U it 5 P k
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 19/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
JD Miles The Java rchive (JD) ,ile ,ormat ena*les to *0ndle
m0ltiple ,iles into a single archive ,ile.
'eed ,or JD ,iles Compression & The JD ,ormat allows compression o, ,iles ,or
e,,icient storage.
Decreased download time & I, a li*rar! is *0ndled in a JD ,ilethe reso0rces can *e downloaded in a single transaction witho0tthe need ,or opening a new connection ,or each ,ile.
Portability - The mechanism ,or handling JD ,iles is a standardpart o, the Java plat,ormNs core PI.
U it 5 P k
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 20/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
reating a JD Mile The *asic ,ormat o, the command ,or creating a JD
,ile isH
jar cvf jar-file input-file(s) The options and arg0ments 0sed in this command
The c option indicates that !o0 want to create a JD ,ile.
The f option indicates that !o0 want the o0tp0t to go to a file rather than to stdo0t.
jarfile is the name that !o0 want the res0lting JD ,ile to have.B! convention JD ,ilenames are given a .ar e>tensiontho0gh this is not reE0ired.
The inputfile!s" arg0ment is a space-separated list o, one ormore ,iles that !o0 want to incl0de in !o0r JD ,ile. The inp0t-,ile(s) arg0ment can contain the wildcard O s!m*ol. I, an! o, the
inp0t-,iles are directories the contents o, those directories areadded to the JD archive rec0rsivel!.
This command will generate a compressed JD ,ileand place it in the c0rrent director!.
Unit 5 Packages
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 21/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
Ciewing contents o, a JD Mile The *asic ,ormat o, the command ,or viewing the contents
o, a JD ,ile isH
jar tvf jar-file The options and arg0ments 0sed in this command areH
The t option indicates that !o0 want to view the table o, contentso, the JD ,ile.
The f option indicates that the JD ,ile whose contents are to *eviewed is speci,ied on the command line.
The jarfile arg0ment is the path and name o, the JD ,ile whosecontents !o0 want to view.
The t and f options can appear in either order *0t there m0st not
*e an! space *etween them. This command will displa! the JD ,ileNs ta*le o, contents
to stdo0t.
Unit 5 Packages
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 22/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
dding a JD ,ile to class path
6hile adding JD ,iles to the classpath it is necessar! to
add not 0st the location *0t also the ,ile name o, the
JD ,ile.
K.g. & onsider a JD ,ile called !Li*.ar located in
HQ!JavaQProectQ!Li*.ar
set classpath9HQ!JavaQProectQ!Li*.ar
nd not
set classpath9HQ!JavaQProectQ
Unit 5 Packages
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 23/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
Please try to limit the questions to the topics discussed during the session. Thank you.
R0estion time
Unit 5 & Packages
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 24/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 & Packages
reating Packages
reating packages involves the ,ollowing three steps
i. #nclude $package%
statement
ii. reate the corresponding ph!sical ,older str0ct0re
iii. +et the classpath environment varia*le
Unit 5 & Packages
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 25/26
Basic Javaop!right " #$$% Pratian Technologies
www.pratian.com
Unit 5 Packages
reating Packages
reating packages involves the ,ollowing three steps
i. Incl0de package statement
ii. Create t&e corresponding
p&ysical folder structure
iii. +et the classpath environment varia*le
Unit 5 & Packages
7/23/2019 Unit5 - Packages
http://slidepdf.com/reader/full/unit5-packages 26/26
B i Jop!right " #$$% Pratian Technologies
Unit 5 Packages
reating Packages
reating packages involves the ,ollowing three steps
i. Incl0de package statement
ii. reate the corresponding ph!sical ,older str0ct0re
iii. 'et t&e $classpat&%
environment variable