Package

  • Package is a grouping of classes and related types providing access protection and name space management.
  • To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages.
  • It helps to organize your classes into a folder structure and make it easy to locate and use them. It also helps to improve re-usability.


Overview of Package

  • Every class is part of some package.

  • All classes in a file are part of the same package.

  • Multiple files can specify the same package name.

  • If no package is specified, the classes in the file go into a special unnamed package.(Same unnamed package for all files).

  • You can access public classes in another package by importing them:
  • import package-name.class-name;

    You can access the public fields and methods of such classes using:
    import package-name.class-name.field-or-method-name;

    You can avoid having to include the package-name using:
    import package-name.*;


Syntax of Package :

     package <package_name>;

Some of the existing packages in Java are

  • java.lang - bundles the fundamental classes
  • java.io - classes for input , output functions are bundled in this package

  • Creating a Package

    • To create a package, you choose a name for the package and put a package statement with that name at the top of every source file that contains the types that you want to include in the package. Types are refer as classes, interfaces, enumerations, and annotation.
    • The package statement (for example, package techknow;) 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.

    • For Example :
      package techknow;
      class Tkh
      {}
      			

    Naming a Package

      Naming Conventions

    • Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
    • Companies use their reserved Internet domain name to begin their package names-for example, com.tkhts.techknow for a package named techknow created by a programmer at tkhts.com.
    • Name collisions that occur within a single company need to be handled by convention within that company, sometimes by including the region or the project name after the company name (for example, com.tkhts.region.techknow).
    • Packages in the Java language itself begin with java. or javax.
    • If you are using internet domain as a package name then it may be possible that it is not a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java package name, or if the package name contains a reserved Java keyword, such as "int". In this situation, the suggested convention is to add an underscore.

    • For example :

      Domain Name Example

        hyphenated-name.tkhts.org

        tkhts.int

        123techknow.tkhts.com

      Package Name Prefix

        org.tkhts.hyphenated_name

        int_.tkhts

        com.tkhts._123techknow


    Using Package Members

      Importing a Package Member
    • By putting an import statement at the beginning of the file before any type definitions but after the package statement, you can import a specific member into the current file. Here's how you would import the Rectangle class from the graphics package.

      import graphics.Rectangle;

      Now you can refer to the Rectangle class by its simple name.

      Rectangle myRectangle = new Rectangle();

    • Importing an Entire Package
    • To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character.

    • import graphics.*;
      import java.awt.*;

      Apparent Hierarchies of Packages
    • Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color, java.awt.font, or any other java.awt.xxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:

    • import java.awt.*;
      import java.awt.color.*;

    Set CLASSPATH:

      To display the current CLASSPATH, use the following commands in Windows and Unix:

    • In Windows -> C:\> set CLASSPATH
    • In Unix -> % echo $CLASSPATH

    • To set the CLASSPATH variable:

    • In Windows -> set CLASSPATH=C:\techknow\tkhts\java\classes
    • In Unix -> % CLASSPATH=/techknow/tkhts/java/classes; export CLASSPATH