Describing the Java Packages

Package in Java 

 

Introduction

  
A package is a grouping of related types providing access protection and namespace management. Here type means classes, interfaces, enumerations, and annotation. Enumerations and annotation types are special kinds of classes and interfaces, respectively. So types are often referred to in this lesson simply as classes and interfaces. A Java package is a mechanism for organizing Java classes into namespaces. Thus Java Packages are containers for classes. That is used to keep the class namespace compartmentalized. In Java package is mapped to a folder on your hard drive.
  
Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality. A package provides a unique namespace for the types it contains. Classes in the same package can access each other's, package-access members. 
 
Thus we can say that
  • Every class is part of some package.
  • All classes in a file are part of the same package.
  • You can specify the package using a package declaration:
    package name; as the first (non-comment) linein the file.
  • Multiple files can specify the same package name.
  • You can access public classes in another (named) package using:
    package-name.class-name
  • You can access the public fields and methods of such classes using:
    package-name.class-name.field-or-method-name
  • You can avoid having to include the package-name using: 
    import package-name.*;
              or
    import package-name.class-name; at the beginning of the file (afterthe package declaration). The former imports all of the classes in thepackage, and the second imports just the named class.
  • You must still use: Class-name to access the classes in the packages, and 
    class-name.field-or-method-name to access the fields and methods of theclass; the only thing you can leave off is the package name.
Some core packages of java are
 
java.lang Basic language functionality andfundamental types
java.util Collection data structure classes
java.io  File operations
java.math Multiprecision arithmetics
java.nio The New I/O framework for Java
java.net Networking operations, sockets, DNSlookups
java.security  key generation, encryption anddecryption
java.sql Java Database Connectivity to accessdatabases
java.awt The basic hierarchy of packages for nativeGUI components
javax.swing Hierarchy of packages forplatform-independent rich GUI
java.applet  Classes for creating an applet
 
Create Its Own package
  
For creating the package we follow the following steps:
  
Defining a Package
  
To define a package, include a package command as the first statement in a Java source file.
  
Any classes declared within that file will belong to the specified package.If you omit the package statement, the class names are put into the default package, which has no name. This is the general form of the package statement:
  
package packageName;
  
Create a hierarchy of packages
  
To create a hierarchy of packages, separate each package name from the one above it by use of a period. The general form of a multileveled package statement:
  
package pkg1[.pkg2[.pkg3]];
  
Example
 
In this example, we create a folder of name MyPack and save this java file with name My1.java .The package name and folder name should be same:
  
My1.java
  1. package MyPack;  
  2. public class My1   
  3. {  
  4.  public static void main(String args[])   
  5.  {  
  6.   System.out.println("Hi Friends");  
  7.   System.out.println("My name is Vikas Mishra");  
  8.   System.out.println("This application is the example of How to create it's own package in java");  
  9.   System.out.println("Hope its help you to understand the package concept");  
  10.  }  
  11. }   
Compile and Run it
  
After running this code will give the output as follow:
  
ownpackage.jpg
  
Using The packages
  
For using the java core packages we Importing the packages and for importing we use the import  statement .In a Java source file, import statements occur immediately following the package statement and before class definitions.
  
This is the general form of the import statement:
 
import pkg1[.pkg2].(classname|*);
 
(*) means including all classes under that package.
 
For example:
  
import java.lang.*;
 
Another Application
  
In this example we are going to import util package to use the class:
  
A. java:
  1. import java.util.Date;  
  2. public class A   
  3. {  
  4.  public static void main(String args[])   
  5.  {  
  6.   System.out.println(new Date());  
  7.   System.out.println("My name is vikas");  
  8.   System.out.println("This is an example of using A java package util");  
  9.  }  
Output
  
After running the above application we'll find the following output: 
 
util.jpg
 
Thus this article may help you to understand the package and it's creation: