Java Package

Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.

java.lang package by default available in Java.

Package Types-
    1. User Defined
       These are the packages that are defined by the user.
    2. Built In
       These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in.

Built IN Packages are: `
    1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported.

    2) java.io: Contains classed for supporting input / output operations.

    3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations.

Packages are used for:
    1. Preventing naming conflicts. For example there can be two classes with name Customer in two packages, com.csi.hdfc.Customer and com.csi.icici.Customer

    2. Making searching/locating and usage of classes, interfaces, enumerations and annotations easier.

    3. Providing controlled access: protected and default have package level access control. A protected member is accessible by classes in the same package and its subclasses. A default member (without any access specifier) is accessible by classes in the same package only.

    4. Packages can be considered as data encapsulation (or data-hiding).

All we need to do is put related classes into packages. After that, we can simply write an import class from existing packages and use it in our program. A package is containers of a group of related classes where some of the classes are accessible are exposed and others are kept for internal purpose.

We can reuse existing classes from the packages as many times as we need it in our program.

Syntax:
  • package com.csi.hdfc.loan.aggreeloan.emi.advance
  • package com.csi.hdfc.loan.aggreeloan.emi.preclosure
  • package com.csi.hdfc.loan.homeloan.emi.advance

As per Industry Standers-
    1. com is operating systems- core level package. This is generally a company specification. In this case, the root folder is “com”.
    2. csi is a company name as Credit Systems India in which product is developed.
    3. hdfc is the client name for which we are developing our product.
    4. loan is our project name.
    5. agree loan and home loan are loan types and so on.
    6. Let it be noted that we can give any names, this is just a specification.
    7. Keep in mind that the root folder should always be same for all classes.