Saturday, August 13, 2016

Package

So far, we use directory D:\javacode only to place our java code. Many times when we get a chance to work on a small project, one thing we intend to do is to put all java files into one single directory. It is quick and easy. However if our small project gets bigger, and the number of files is increasing, putting all these files into the same directory would make complication. In java we can avoid this problem by using package.

A package is a namespace that organizes a set of related classes. Conceptually you can think of package as being similar to different folders on your computer. Package is the way we organize files into different directories according to their functionality, usability as well as category they should belong to. Package also helps us to avoid class name collision when we use the same class name as that of others. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers.


Create a Package


To create a package, you choose a name for the package and put a statement package with that name at the top of every source file that you want to include in the package. The package statement must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

Suppose we have two files named Circle.java and Triangle.java, and we want to put this file in a package geometry. First thing we have to do is to specify the keyword package with the name of the package we want to use (geometry in our case) on top of our source file, before the code that defines the real classes in the package, as shown in our Circle and Triangle class below:

package geometry;

public class Circle {

   private final double PI = 3.1416:

   private double r;

   public Circle(double r) {
      this.r = r;
   }

   public double area() {
      return (PI * r * r)
   }

   public double circumference() {
      return (2 * PI * r);
   }
}


package geometry;

public class Triangle {

   private double height;
   private double base;

   public Triangle(double height, double base) {
      this.height = height;
      this.base = base;
   }

   public double area() {
      return ((base * height) / 2);
   }
}


One thing you must do after creating a package for the class is to create nested subdirectories to represent package hierarchy of the class. In our case, we have the package geometry, which requires only one directory. So, we create a directory geometry and put our Circle.java and Triangle.java into it. Compile those two files, then we will get Circle.class and Triangle.class.




How to Use Package


To use classes stored in package, use keyword import. Now we will create CallPackageDemo class in directory D:\javacode and use Circle and Triangle class in package geometry. Create CallPackageDemo.java in D:\javacode directory with following program:

import geometry.*;

class CallPackageDemo {

   public static void main(String[] args) {

      Circle circle = new Circle(7);
      Triangle triangle = new Triangle(4, 3);

      System.out.printin("Circle area : " + circle.area());
      System.out.printin("Circle circumference : " + circle.circumference());
      System.out.printin("Triangle area : " + triangle.area());
   }
}


Compile and run CallPackageDemo, then the result that will be given by the above program as follows:

Circle area : 153.9384
Circle circumference : 43.9284
Triangle area : 6.0

Instead of call all package content using asterisk (*) like this following statement:

import geometry.*;

You can also call the class inside the package one by one like this following statement:

import geometry.Circle;
import geometry.Triangle;

4 comments:

  1. Thank You for Sharing valuable information with us.

    E-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.

    For more details please visit our website : Aptech Janakpuri

    ReplyDelete