What are Packages and How we can use it?
Tags: java, packages, datastructures, productivity
Published: 2023-11-01 10:29:14

In this article, We will learn about packages and What is the hierarchy and learn about how Packages contains different class and  methods. This term packages used in every programming languages, so  I though to share my knowledge and some examples to related to topic.

With term Packages what comes in our mind. Just think once and let me know in the comment. So, what you think that's right!!! 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrj1uev05woe8e4e035s.jpg)

Example :- Packages is same as you might think in general wrapper or box. 

**But What we say in technical term, What are Packages?**

**Something that's Packed** **Right!!!**

Packages is set of programs files and data files which developer by programmers to perform specific task and include information regarding installation of packages.

**Or We can say...**

A package is a container of a group of related classes where some of the classes are accessible are exposed and others are kept for internal purpose.
We can reuse existing classes from the packages as many time as we need it in our program.

**Here is the hierarchy of Packages -> Sub-Packages-> classes**
You can visualize how it is structured inside single package java.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oh9r4u4ss2aswaqwer6q.jpg)

 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1yzzt4x6sle96dv0cel8.jpg)

Now, You had a Clear understanding of the what are packages and how it is structured. Now, We move forward and go deep down in packages and it's type? How it works?

## Types of Packages:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8rsi43t148liluj0u7q.jpg)

## User Defined Packages
These are the packages that are defined by the user. First we create a directory myPackage (name should be same as the name of the package). Then create the MyClass inside the directory with the first statement being the package names.

for Example :- 

```
// Name of the package must be same as the directory
// under which this file is saved
package myPackage;

public class MyClass
{
    public void getNames(String s)
    {        
        System.out.println(s);        
    }
}
```
Now, we can use above MyClass class in our program.

```
/* import 'MyClass' class from 'names' myPackage */
import myPackage.MyClass;

public class PrintClass 
{
   public static void main(String args[]) 
   {       
      // Initializing the String variable with a value 
      String name = "Casto";
      
      // Creating an instance of class MyClass in the package.
      MyClass obj = new MyClass();
      
      obj.getNames(name);
   }
}
```
Note :- MyClass.java must be saved inside the myPackage directory since it is a part of the package.

## Builtin Packages

BuiltIn Packages consist of large number of classes which is defined by java or certain programming language to perform necessary operations.
Built-in packages in Java consist of classes that are part of the Java API.

Some of the commonly used built-in packages used in java are:-

1) java.lang:- Contains language support classes.This package is automatically imported.
2)  java.io:- Contains classed for supporting input / output operations.
3)  java.util:- Contains utility classes which implement data structures like Linked List, Dictionary and support,for Date / Time operations.
4)  java.applet:- Contains classes for creating Applets.
5)  java.awt:- Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).
6)  java.net:- Contain classes for supporting networking operations.

## Using Static import

Static import is a feature introduced in Java programming language ( versions 5 and above ) that allows members ( fields and methods ) defined in a class as public static to be used in Java code without specifying the class in which the field is defined.

```
import static java.lang.System.*; 

class ImportDemo 
{ 
public static void main(String args[]) 
{	 
		// We don't need to use 'System.out' 
		// as imported using static. 
		out.println("GeeksforGeeks"); 
} 
} 
```

## Handling Name Conflicts
The only time we need to pay attention to packages is when we have a name conflict . For example both, java.util and java.sql packages have a class named Date. So if we import both packages in program as follows:

```
import java.util.*;
import java.sql.*;

//And then use Date class, then we will get a compile-time error :

Date today ; //ERROR-- java.util.Date or java.sql.Date?
```
The compiler will not be able to figure out which Date class do we want. 
This problem can be solved by using a specific import statement:

```
import java.util.Date;
import java.sql.*;
```

If we need both Date classes then, we need to use a full package name every time we declare a new object of that class.
For Example:

```
java.util.Date deadLine = new java.util.Date();
java.sql.Date today = new java.sql.Date();
```

## Important Points to Remember :-

1- Every class is part of some package.
2- If no package is specified, the classes in the file goes into a special unnamed package (the same unnamed package for all files).
3- All classes/interfaces in a file are part of the same package. Multiple files can specify the same package name.
4- If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
5- We can access public classes in another (named) package using: package-name.class-name