Packages

Package: A collection of related .class files.[We know that the .class files can be generated to both classes and interfaces.So, we can say a package as a collection of classes and interfaces].

-A package specifies a new namespace,where we can place our own related files.
-Modularity is supported with packages.
-Java supports 2 kinds of packages:
    1. Built-in packages
    2. User defined packages

Built-in packages:
  • Comes with java software. Ex: java.io, java.util, java.net, java.awt..etc.,
  • All these packages support and provide the corresponding predefined functionalities which are helpful to develop applications.
  • Ex: java.io----> for handling input/output operations.
User defined packages:
  • These are created by us.
Creating a user defined package:
      Use the following syntax:
                             package package_name; 
Note: This statement must be kept as a first statement in our source files.




To do this exercise:
  • First create our source file(A.java)
  • Compile that file.
  • Execute that .class file to see the result.
A.java
package myPack;
public class A{
//display method
public int display(){return 100;}
//main method
public static void main(String as[]){
A x=new A();
System.out.println(x.add());
}
}

Note:
         -If you have a public class in your file then you must use that class name to save that file.[If it is not there then you can use any name to save]
         -Within a single file you should have only one public class.[If you want to declare another class as also public then take a separate file in that you can declare]

Compilation:
c:\vvb> javac -d . A.java
After compilation we get the above directory/folder structure[i.e we are creating our own package named myPack in c:\vvb directory,which is our current working dirctory.]

Note: If you compile like c:\vvb>javac A.java then it generates .class files with in the currect working directory,without creating package that is what we specified as a first statement in our source file.

Execution:
c:\vvb>java myPack.A
then it shows the result.

Scenario2:[Add new file B.java.If you compile this B.java then you have to get a directory named myPack2,which contains B.class]

 Exercise: Access add() method of class A in B.Then execute B.class and you should display 100.
















Scenario3:[Add new file C.java in another directory.If you compile this C.java then you have to get a directory named myPack3,which contains C.class file]

Exercise: Access add() method of class A in class C.Then execute C and display the result.


In scenario3,we need to make available our .class file[A.class] to both java compiler and java run time system we have to set CLASSPATH.

Ex:
 D:\xyz>set CLASSPATH=C:\vvb;.;
Now compile and execute,
     D:\xyz>javac -d . C.java
     D:\xyz>java myPack3.C

Creating package hierarchy:
Syntax:
      package pkg1[.pkg2[.pkg3]];
Ex: package pk1.pk2.pk3.pk4;
If you declare a package statement like the above then compiler will create the package structure like:
Okay,In this case how we refer the particular class?
Ex: If you have Z.class in pk4.And now I want to refer Z class in X class,How?

   Then to refer that Z.class file,we do in X like:
pk1.pk2.pk3.pk4.Z    x=new    pk1.pk2.pk3.pk4.Z();
                                  x.add();//in Z we have add() method
i.e in this case to refer a class, we have to use fully qualified name of that class.[fully qualified means that along with its enclosing package names]




 

Comments

Popular posts from this blog

Type Conversion and Casting

Data types: