Java package


Introduction:

A package is a group of similar types of classes, interfaces and sub-packages.
It is a way to achieve reusability. We can simply import a class providing the required functionality from an existing package and use it in our program.
            


Built-in Packages:

It consists of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are shown in the figure below :

                        

To learn about lang package click here
To learn about io package click here


User Defined Packages:

User can create packages and use them. The package keyword is used to create package.
The syntax is :
package package_name;
class class_name
{
Statements;
}


1) Create Sum class in aritmetic package:


2.Save the package.

3.Compile the package using command prompt with command javac -d . classname.java

                        

4.To run the class we have to specify the package name as well.

                        


Access the package:

There are three ways to access the package from outside:

1) import package.*;
By using * all the classes and interfaces of the package will be accessible.



2) import package.classname;

By using ".classname"" only declared class of this package will be accessible.



3) Static Import:
The static import feature was introduced in Java 5. It helps in accessing any static member of an imported class directly i.e. without using the class name.

To learn more about static import Click here