---
title: "Package in Java"  
description: "A package is a namespace that organizes a set of related classes and interfaces."  
author: "Manoj Pandey"  
published: 2015-04-24  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1745/package-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Package in Java

A [package](https://www.mindstick.com/blog/12619/tips-for-finding-the-right-package-when-you-buy-instagram-followers) is a [namespace](https://www.mindstick.com/articles/28/namespace-in-dot-net) that organizes a set of related classes and [interfaces](https://www.mindstick.com/articles/26/interfaces).\

Package is a [collection](https://www.mindstick.com/articles/1718/collections-in-java) of classes, interfaces, [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) sub [packages](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) etc.

##### Some of the existing packages in Java are:

##### \
· java.lang - bundles the fundamental classes

##### \
· java.io - classes for input , output functions are bundled in this package

##### \
Advantages of packages -:

· Reusability: Reusability of code is one of the most important [requirements](https://www.mindstick.com/articles/12850/drivers-license-requirements) in the [software industry](https://www.mindstick.com/blog/11136/patterns-of-success-in-the-indian-software-industry). Reusability saves time, effort and also ensures consistency. A class once developed can be reused by any number of [programs](https://www.mindstick.com/forum/157528/how-exceptions-can-be-handled-in-sql-server-programs) wishing to incorporate the class in that particular program.

· Easy to locate the files.

· In real life situation there may arise scenarios where we need to define files of the same name. This may lead to “name-space collisions”. Packages are a way of avoiding “name-space collisions”.

##### \
Types of package:

##### \

1. User defined package: The package we create is called user-defined package.

##### \
2. Built-in package: The already defined package like java.io.*, java.lang.* etc are

##### known as built-in packages

\

```
// define packagepackage packagename;// import packagesImport mypack;//class declarationclass Sample{// instance members//method implementation}}
```

![Package in Java](https://www.mindstick.com/mindstickarticle/b535b52e-0dad-4d12-b2cd-9004d1755ace/images/63ab86ae-bc73-4cf4-903b-16b06ade0a28.png)

##### Define a package

package <package name>;

##### Example of package of creation

```
package mypack;class Book{ String bookname; String author; Book(String b, String c) {  this.bookname = b;  this.author = c; } public void show() {  System.out.println(bookname+" "+ author); }} class test{ public static void main(String[] args) {  Book bk = new Book("java","Herbert");  bk.show(); }}
```

##### Access of package-:

Import.mypack;

**import** java.util.Date;

## Subpackage in java-:

Package inside the package is called the subpackage. It should be created to categorize the package further.

Example of sub packages

```
package com.midstick.core;  class Sample{    public static void main(String args[]){     System.out.println("Demo subpackage");    }  }
```

\

```
package com.mindstick.coreimport static java.lang.Math.*;public class Test{    public static void main(String[] args)    {        System.out.println(sqrt(144));    }}
```

output -: 12

---

Original Source: https://www.mindstick.com/articles/1745/package-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
