Tag: packages in java

Packages in Java, Introduction – Part 1 or Parts 2

java-logo

Packages

  1. A java package is a group of similar types of classes, interfaces and sub-packages.
  2. A sub-package is again group of similar type of classes, interfaces and sub-packages.
  3. Pakcage in java can be categorized in two types,
    1. Built-in package
    2. and User-defined package

Advantages of Java Package

  1. Easy to Maintain: Java package is used to categorize the classes and interfaces so that they can be easily maintained.
  2. Easy to organize: It is easy to organize class files into packages.
  3. Provide Protection: Java package provides access protection.
  4. Avoid Collision: Java package removes naming collision.
  5. Avoid Conflicts: Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.
  6. Reusability: most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be resued by any number of programs wishing to incorporate the class in that particular program.

Types of package:

User defined package:

User-defined packages are created by user as project demands based on its requirements. The package we create is called user-defined package.

Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.

There are many built-in packages in Java JDK such as java, lang, awt, javax, swing, net, io, util, sql etc

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
  • Java.util.

Where do we include Package in a typical Java program:

This statement should be used in the beginning of the program to include that program in that particular package.

Example:

package tools;

public class Employee {
public void empName ()
{
System.out.println (“Hello there”);
}
}
**Note:

  1. At most one package declaration can appear in a source file.
  2. The package declaration must be the first statement in the unit.
Advertisements