Wednesday 28 August 2013

Packages

Packages are also known as container. In the package we create classes of similar nature.
In package , all classes should have unique name. Two different package can contain classes of same name.

please check the location of the folder in the address bar in all examples.....
it will help you in compilation.

Creation of package....

first of all create two folder:-
1)javafile
2)class
           like this:-

in the javafile folder create 1 folder name p1  .which acts as a package.
now save your .java file into p1.

Example:-


here we define package p1 by using keyword "package". p1 is the package name.
-d:-    to separate the files location
..  :-     take 1 directory back
/class   :-   insert into class folder
p1/V.java  :-compile V.java along with package and generate class file in class folder....

after compilation it automatically creates p1 folder in class folder, under p1 it creates the V.class file..

check the location in url under class folder it creates p1 folder....
please check the location of the folder in the url in all examples.....
which is used in compilation.

calling class of different packages

first we create second package p2..make folder p2 in javafile folder....
now write the code



on compilation we automatically get p2 folder in class folder
                                         
                                                             

now we call class C of package p2 into package p1.
there are 2 methods to call the class

1)
 2)


by this we create the packages which is used for making the collection of class of similar nature....

for creating sub packages makes a new folder  inside the folder which was created in the javafile folder.

Example:-main folder -javafile =>inside javafile create pack1 folder=>inside pack1 create another folder subpack1.
subpack1 is the subpackages.
when you import the subpackages write  pack1.subpack1.* which import all files present in subpackage






No comments :

Post a Comment