Friday, 31 May 2013

Packages in JAVA

Definition: A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Packages helps Organize your classes into a folder structure and make it easy to locate and use them. More importantly, packages helps improve re-usability.
Syntax:- package <package_name>;
*******************************************************
Example1: To create a package

Step 1: Copy the following code into an editor
package p1;
class c1{

public void m1(){
System.out.println("Method m1 of Class c1");
}

public static void main(String args[]){
c1 obj = new c1();
obj.m1();
}
}
Step 2: Save the file as Demo.java.
Step 3:  Compile the file as, javac – d . Demo.java
Step 4: Run the code as java p1.c1
*******************************************************
Example2: To create a sub-package
Step1:  Copy the following code into an editor
package p1.p2;
class c2{
public void m2(){
System.out.println("Method m2 of Class c2");
}
public static void main(String args[]){
c2 obj = new c2();
obj.m2();
}

}
Step 2:  Save the file as Demo2.java.
Step 3:  Compile the file as javac – d . Demo2.java
Step 4:  Run the code as java p1.p2.c2
*******************************************************

The "import" Keyword:
If a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other without any special syntax.

Example:
Here a class named Manager is added to the payroll package that already contains Employee. The Manager can then refer to the Employee class without using the payroll prefix, as demonstrated by the following Manager class.

package payroll;

public class Manager
{
   public void payEmployee(Employee e)
   {
      e.mailCheck();
   }
}

What happens if Manager is not in the payroll package?
The Manager class must then use one of the following techniques for referring to a class in a different package.

The fully qualified name of the class can be used. For example:

payroll.Employee

The package can be imported using the import keyword and the wild card (*).

For Example: import payroll.*;

The class itself can be imported using the import keyword.

For Example: import payroll.Employee;

Note: A class file can contain any number of import statements. The import statements must appear after the package statement and before the class declaration.

Following is the example to show how do we access the class that is not existing in same package using import keyword.
**********************************************
Step:1
package com.sample.java.testing;

public class PackageExample1 {

public String getName() {
System.out.println("getName of PackageExample1");
return "PackageExample1 method";
}

public static void main(String s[]) {
PackageExample1 example1 = new PackageExample1();
example1.getName();
}
}

Step 2: Save the file as PackageExample1 .java.
Step 3: Compile the file as, javac – d . PackageExample1.java. 
Then it will create class file under folders 
com/sample/java/testing/PackageExample1.class if no compile time errors.
Step 4: Run the code as java com.sample.java.testing.PackageExample1

*****************************************

package com.sample.javase.testing;
//need to write this import statement if you want to use a class not exist in the same package
import com.sample.java.testing.PackageExample1;

public class PackageExample2 {

public void getName(PackageExample1 example1) {
System.out.println("getName of PackageExample2" + example1.getName());
}

public static void main(String s[]) {
PackageExample1 example1 = new PackageExample1();
PackageExample2 example2 = new PackageExample2();
example2.getName(example1);
}
}


********************************

No comments:

Post a Comment