One of the reasons for the popularity of Java is the support of standard class libraries called packages. A java package is a group of similar types of classes and interfaces. Packages in Java are a way of grouping together related classes and interfaces. Packages act as containers for classes and interfaces.
A collection of
classes and interfaces is known as Packages. Packages
are used in Java in order to prevent naming conflicts, to control
access, and to write a better maintainable code. It provides a
convenient way to organize your work. The classes and interfaces of
a package are like books in the library that can reuse several times when we
need them. This reusability nature of packages makes programming easy.
Java packages are classified into two types:
1. Built-in packages ( Java API packages )
2. User-defined packages ( Create your own package)
Built-in Packages (Java API packages):
Built-in packages are existing java packages that come along with the JDK.
The Java API provides a large number of predefined
classes grouped into different packages according to functionality.
There are many built-in packages in java. Few of them frequently used in the program are given below:
Package name |
Contents |
java.lang |
Language support classes |
java.util |
Language utility classes |
java.io |
Input/output support classes |
java.awt |
Classes for implementing graphical user interface |
java.net |
Classes for networking |
java.applet |
Classes are creating and implementing applets |
We need to import the built-in packages to use them in our program. To import a package, we use the import statement.
User-Defined Package:
The package
which is defined by the user is called a User-defined package. It contains
user-defined classes and interfaces. The user-defined packages are created by the user. Users are
free to create their own packages. Java
allows you to create packages as per your need. Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a
good practice to group related classes implemented by you so that a programmer
can easily determine that the classes, interfaces are related. Using packages,
it is easier to provide access control and it is also easier to locate the
related classes.
Creating a Package:
While creating a package, you should choose a name for the package and include a package statement along with that name at the top of every source file that contains the classes and interfaces.To create a package in Java, you use the keyword package.
package packageName;
Here, packageName is the name of package. The package statement must be
the first line in a java source code file followed by one or more classes.
Creating a package is a simple task as follows:
- Choose the name of the package
- Include the package command as the first line of code in your Java Source File.
- The Source file contains the classes, interfaces, etc you want to include in the package
- Compile to create the Java packages
0 Comments
if you have any doubts plz let me know...