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 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,

  1. User defined packages
  2. In Built 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.


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.
  • 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 .
  • bundles the classes for supporting network operations.

These all are in-built packages that are commonly used.

MERITS of packages in java

  1. By the use of packages in java, it becomes easy to search and locate any class, annotation, enumeration etc.
  2. Naming conflict can be prevented that are occurred in between the different classes by the use of java packages.
  3. Java packages renders protection.
  4. 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.
  5. Reduction in execution time i.e. execution time is less.
  6. Uses less memory space.
  7. Improved performance.

Steps for creating a user defined package:

  1. Package program’s first statement should be the package statement.
  2. Class modifier must we public so that the class and methods can be used outside the program.
  3. Only one public class or only one public interface are used in package program while any number of normal classes are used in it.
  4. It should contain any main class not the main () in it.
  5. Constructor modifier must be Public.
  6. Method modifier of class or interface must be public.
  7. The package program should be save either with public class name or a public interface name.


//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



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.