Home »
Java programming language
Java Packages | Types, Usages, Example
Learn: In this article, we will learn about different packages in java programming language.
Submitted by Shivangi Jain, on May 01, 2018
Packages
Packages in Java is simply a mechanism to encapsulate (i.e. to put in a short and concise form) a group of classes,interfaces,enumerations, sub packages, etc. In real world, application is developed in such a manner so that we can easily maintain each module. To create package is simply use package keyword with name of the package at first statement in the program.
There are two type of Packages that are found in java,
- User defined packages
- In Built packages
1) USER DEFINED PACKAGES
The Packages that are created by the user to differentiate between the classes and the interfaces that are made in their projects are user defined packages.
2) IN-BUILT PACKAGES
The Packages that are the part of java API’s and includes variousclasses, interfaces, sub packages that are already defined in it are in-built packages. These packages are also known as the Predefined packages.
There are some packages that exists in java, they are:
- java.lang: uses to bundles the fundamental classes.
- java.io: classes for input , output functions are bundled in this package.
- java.util: classes which are implemented in data structure for date and time operations are bundled here.
- java.applet: bundles classes for making applets .
- java.net: bundles the classes for supporting network operations.
These all are in-built packages that are commonly used.
MERITS of packages in java
- By the use of packages in java, it becomes easy to search and locate any class, annotation, enumeration etc.
- Naming conflict can be prevented that are occurred in between the different classes by the use of java packages.
- Java packages renders protection.
- Most of programming tasks are done by the API’s classes and Packages, which minimize the number of lines that are written within the piece of code.
- Reduction in execution time i.e. execution time is less.
- Uses less memory space.
- Improved performance.
Steps for creating a user defined package:
- Package program’s first statement should be the package statement.
- Class modifier must we public so that the class and methods can be used outside the program.
- Only one public class or only one public interface are used in package program while any number of normal classes are used in it.
- It should contain any main class not the main () in it.
- Constructor modifier must be Public.
- Method modifier of class or interface must be public.
- The package program should be save either with public class name or a public interface name.
Syntax:
//Sum.java
//save package with 'public' classname
//first statement is package
package OurPackage
//class modifier must public
public class Sum {
//constructor modifier must public.
Public Sum() {
System.out.println("Sum class constructor");
}
//method modifier must public.
Public void show() {
System.out.println("Sum class method");
}
}
Read more: Packages in Java