Definition

  • A package is a collection of similar type of related sub-packages, classes, interfaces, enumerations, annotations etc. in separate bundle that provide easy access, security, protection and namespace (storage place) management.

Features/Characteristics 

  • A package behaves like as container which mainly stores similar type of related sub-packages, classes and interfaces.
  • We can reuse the methods of a class & interface of the package or sub package many times as per requirements in our java program by importing those package once as the first statement or beginning of the java program using import keyword.
  • When a package creates another package inside it (for better management), is called the sub package.
  • Package shows encapsulation features in which various related sub-packages, classes & interfaces are encapsulated in separate bundle.
  • Classes & Interfaces with the same name cannot put in the same package but they can appear in different packages.

Rules/Conventions 

  • There must be not more than one public class per package file.
  • package’ keyword is used to create new user defined package.
  • We can use one or more than one import statements (in the beginning/first line of the program) to include different packages in a java program, as per need.
  • Package declaration should be in the first line and package import should be the second line statement in a java program.
  • In a class, we can create/declare only one package but a class can import more than one package as per need.
  • Package is saved/stored in a directory or folder having same name as package.

Advantages

  • Package is used to organize/store multiple related classes, sub-packages & interfaces mainly so that they can be reused/retrieved/maintained easily and quickly. Thus, they provide reusability of methods multiple times when needed in the program and also support better organization for classes inside packages.
  • Package supports access protection i.e. they support control access for selected objects using protected and default keyword.
  • Each package in Java has unique name and manage the classes and interfaces into a separate namespace/name group for simplification.
  • Package helps to avoid/remove name conflicts or collision by defining two or more classes with the same name but in different packages.
  • Making searching fast & easy for classes, interfaces etc.
  • Package supports data encapsulation (or data-hiding) feature.

Disadvantages

  • We cannot pass parameters/arguments in a package.
  • Package program is comparatively slower and consuming more memory during execution than native application program such as C, C++ etc.
  • The default look and feel of GUI applications written in Java using the Swing/AWT toolkit is slightly different from native applications.

Types of Packages 

Packages are of two types –

(A)System Defined / In-Built / Built-in Package:

    • This package already come with java application when installed or with java API and hence no need to install separately.
    • Some most common usable built-in packages are –

(a) java.io : 

This package contains classes having input output related methods used in java program for input output statement.

(b) java.util : 

This package contains classes having various utilities related methods which implement several data structures in the program. e.g.- use of  Date/Time .

(c) java.lang : 

This package contains classes having language support (classes that contains primitive data types, math operations etc.) related methods used in java program for primitive datatype statement. This is default java package and is automatically imported in a program and not necessary to import this package in a class.

(d) java.applet : 

This package contains classes having applet related methods that may use in java program in applet creation.

(e) java.awt :

This package contains classes having graphical user interfaces (like textbox, button, menus, combo box, list box, label etc.) methods normally required in java program in interface programming.

(f) java.net : 

This package contains classes having various network supporting methods used in advance java programming for data communication operations.

(B)User Defined Package:

  • This package is created by the user as per requirement and can be used in another java program.

Syntax

 To Compile Java Package

A java package is compiled through two ways-

(A) Through command line or DOS mode

(B) Through IDE (such as Net Beans, Eclipse etc.)

(A) Through Command line or DOS mode:

Syntax=c:\javac -d . savedjavafilename with extension  

eg.=c:\javac -d . employee.java  

NB: Here -d (directory) switch says the compiler where is specifies destination directory/ folder to store/save the generated class file. In place of –d, we can also use any valid directory path name as we already created such as /emp (in case of Linux), c:/emp (in case of windows) etc.

To store the created java package class file within the same or current directory as package name, we use. (dot) operator.

(B) Through an IDE:

The IDE may be Net Beans IDE/Eclipse etc. 

To Run/Execute/Interpret Java Package File/Program

                Syntax=C: \Java classfilename              

                eg.=

 Important Notes

 A user created class in java is always either a default package (when package is not defined) or may be a part of any user defined package.

  • If no package is specified/defined for a java program, then that java program is stored automatically into a special unnamed package called default package.
  • All system defined and user defined classes/interfaces in a java file must be a part of a package either defined package or default package.
  • A user created class in java is always either a default package (when package is not defined) or may be a part of any user defined package.
  • If no package is specified/defined for a java program, then that java program is stored automatically into a special unnamed package called default package.
  • All system defined and user defined classes/interfaces in a java file must be a part of a package either defined package or default package.

Class Path

 489 total views,  2 views today

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.