# 5) Packages

#### Real World Example:

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGd_Aj7MflWiRX1jFG%2F-LTGjKi8gvbeAFodteuo%2Ffacade-design-pattern-latest.001.jpg?alt=media\&token=754484ac-46a8-44b5-b047-4535cabb02b7)

#### Use in Java:

Use a package to group together a related set of classes and interfaces.

#### How to Create package in Java?

You can indicate that a class or an interface is defined in a package by using the package statement as the first statement in code.

A package is made of multiple sections that go from the more-generic (left) to the more-specific (right).For example:

```java
package com.oracle.javacert.associate;
class ExamQuestion {
    // variables and method
}
```

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGd_Aj7MflWiRX1jFG%2F-LTGlIMoeG0ILhl0EMBd%2FScreen%20Shot%202018-12-09%20at%2012.31.01%20PM.png?alt=media\&token=c7405f28-4aca-491d-8a23-1d5ee1ead2ec)

**Ex 2:**

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGd_Aj7MflWiRX1jFG%2F-LTGl_Sr4ZNujCC5EkSJ%2FScreen%20Shot%202018-12-09%20at%2012.32.15%20PM.png?alt=media\&token=7b8554b7-6a52-4af9-9dcf-2b312d9675e0)

#### **Rules to remember:**

* Per Java naming conventions, package names should all be in lowercase.
* The package and subpackage names are separated using a dot (.).
* Package names follow the rules defined for valid identifiers in Java.
*   For classes and interfaces defined in a package, the package statement is the

    first statement in a Java source file (a .java file). The exception is that comments

    can appear before or after a package statement.
*   There can be a maximum of one package statement per Java source code file

    (.java file).
*   All the classes and interfaces defined in a Java source code file are defined in

    the same package. They can’t be defined in separate packages.

#### Directory Structure:

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGd_Aj7MflWiRX1jFG%2F-LTGmKZQ0-oMiNdCm3II%2FScreen%20Shot%202018-12-09%20at%2012.35.18%20PM.png?alt=media\&token=02c134c3-f0c8-4d8e-b11b-19241025c7c3)

####

### Import Statements:

The import statement enables you to use simple names instead of using fully qualified names for classes and interfaces defined in separate packages.

#### Real World Example:

**Problem statement:** Need to access living room of home from office cubicle.

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGoA6X7r8Vz9yvge8b%2F-LTGoYNR6BPABkazViJQ%2FScreen%20Shot%202018-12-09%20at%2012.45.20%20PM.png?alt=media\&token=7327262b-2e20-45d9-a532-4a0a08ad8d7d)

**Solution:**

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGoA6X7r8Vz9yvge8b%2F-LTGov2o_ZpG7NzBYAZ_%2FScreen%20Shot%202018-12-09%20at%2012.46.14%20PM.png?alt=media\&token=0a8079cd-0341-44fe-89a6-369e4690dde9)

**Explanation**:

To refer to the LivingRoom in Cubicle, you must specify its complete location, as shown in the left part of the figure. As you can see in this figure, repeated refer- ences to the location of LivingRoom make the description of LivingRoom look **t**edious and redundant. To avoid this, you can display a notice in Cubicle that all occurrences of LivingRoom refer to LivingRoom in Home and thereafter use its simple name. Home and Office are like Java packages, and this notice is the equivalent of the import statement.Above figure shows the difference in using fully qualified names and simple names for LivingRoom in Cubicle.

#### Implementation In Java:

**One way (Left part of figure)**

```java
package office;
class Cubicle {
    home.LivingRoom livingRoom;
}
```

**Second way (Right part of figure)**

```java
package office;
import home.LivingRoom;
class Cubicle {
   LivingRoom livingRoom;
}
```

```
Note: 
The import statement doesn’t embed the contents of the imported class in your class, 
which means that importing more classes doesn’t increase the size of your own class.
```



#### Important Java Packages:

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGqtudDZbeByMMsYp3%2F-LTGr9IXFC7KifhbdV5v%2FScreen%20Shot%202018-12-09%20at%2012.56.39%20PM.png?alt=media\&token=36eaf418-4120-46bd-b6a6-0a6ebb1a0ffa)



**Importing a single member versus all members of a package:**

By using the wildcard character, an asterisk (\*), you can import all the public members, classes, and interfaces of a package.

Let say we have 2 classes **ExamQuestion** and **MultipleChoice** in package certification.

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGuJWQ6AgCGUouJM1D%2F-LTGv6DU5Ta1kUUSyjrQ%2FScreen%20Shot%202018-12-09%20at%201.13.14%20PM.png?alt=media\&token=dae71670-5778-4592-88a6-e2c9f0e8a657)

```java
import certification.ExamQuestion;
public class AnnualExam {
	ExamQuestion eq;
	MultipleChoice mc;
}

```

```
Above code will not compile as class MultipleChoice has not been imported.
```

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTGuJWQ6AgCGUouJM1D%2F-LTGvl_YcZUK3n8_lD2F%2FScreen%20Shot%202018-12-09%20at%201.16.36%20PM.png?alt=media\&token=0c8512f1-6905-413c-9b98-9921dddefc07)

```java
import certification.ExamQuestion;
import certification.MultipleChoice;

public class AnnualExam {
	ExamQuestion eq;
	MultipleChoice mc;
}

```

```
Above code will compile
```

\
