Java Tutorial – Java Packages

Java Tutorial – Java Packages

What is a Package?

Java Packages are used to bundle and organize Java classes and interfaces into namespaces to provide access protection and namespace management. In other words, we use Java packages to bundle groups of related classes, interfaces and enumerations into packages as to avoid naming conflicts and to control access. Java uses the filesystem to manage source and class files in a hierarchical fashion. So if your package is called com.avaldes.util, Java will create the folders (directories) called com/avaldes/util where the class files will ultimately reside.

The following illustration should help clarify things a bit.

java_package_hierarchy

The Package File Structure

As you can see, our package directory resides inside of our source root directory (src). In our case, it begins with the com package as this was our TLD (Top-Level Domain). Inside this package (folder), we have our avaldes package which contains our remaining three packages model, service, and util.

java_package_filestructure

As previously discussed in the “Java Tutorial – Language Syntax and Structure” post, the dot (.) notation is used to identify members in the Java package hierarchy. We are free to use the Customer class by prepending the com.avaldes.model package namespace: com.avaldes.model.Customer. This is called the fully qualified name.

Java uses packages extensively, for example: java.nio.*, java.text.*, java.util.logging.*, and java.util.*

What’s Covered

  1. Java Packages Naming Conventions
  2. Creating a Java Package
  3. Advantages of using Java Package
  4. Importing Classes from Java Package
  5. Importing All Classes from Java Package
  6. Using Fully Qualified Name to use Classes from Another Package

Java Packages Naming Conventions

Java Packages are always written in lowercase and use the reverse domain name order. For example, if your company was avaldes.com and I wanted to create a package called tutorial then it would be written as com.avaldes.tutorial the general format is, tld.domain.package.subpackage. The TLDs currently in use are .com, .net, .org, .mil, .gov, .edu or one of the two-letter ISO country codes like .us, .in, .ca, .jp, .cn.

For more information on Java Naming Convention, Language Syntax and Structure

Creating a Java Package

When we define our Java classes we can specify which Java Package the class be placed into.

    package <fully qualified package name>

Key Points

  1. It is recommended to use the reverse domain name of your company as the top level package (ex: avaldes.com becomes com.avaldes)
  2. The package declaration must be the first statement in a Java source file
  3. There can only be one package declaration in a Java source file

Here is how you would declare the package in a Java source file.

package com.avaldes.tutorial.model;

public class Customer {
 ...
}

In this sample code above, you will notice the first line in the Customer class declaring the package com.avaldes.tutorial.model.

Advantages of using Java Package

  1. The main advantage of using Java Package is the organizational aspect of grouping all logical classes in a specific package. This allows you to easily determine which files are related.
  2. Another main reason to use packages is that doing so allows you to avoid name collisions in classes. These name collisions may occur if we use a class, say Customer, created by one developer that collides with the same named class by a another developer. Without packages the Customer class would be put into the default package causing the name collision.
  3. We can improve application download time by storing the Java package in the compressed JAR files.
  4. By using Java packages you can allow types within it to have unrestricted access to one another yet still restrict access for types outside the package.

Importing Classes from Java Package

We can access the package members (the types that make up a package), that have been defined in one of three ways.

  1. By using the fully qualified name of the member
  2. By importing the package member
  3. By importing all of the members in the entire package (more on this one later)

    import <fully qualified package name>

Key Points

The import statement` must be the first statement in a Java source file.

The following Java source code examples will help illustrate how Packages are used. In the first case, we will assume the members both reside in the package. In this example, we have an interface called Animal which resides in the com.avaldes.tutorial package and has three methods: eat(), speak() and sleep(). The Dog class subsequently resides in the same package and as such the import statement is not required.

Animal Interface in Same Package

package com.avaldes.tutorial;

public interface Animal {
  public void eat();
  public void speak();
  public void sleep();
}

Dog Class in Same Package

package com.avaldes.tutorial;

public class Dog implements Animal {
  public void eat() {
    System.out.println("I love bones...");
  }

  public void speak() {
    System.out.println("Woof, Woof.");
  }

  public void sleep() {
    System.out.println("Sleeping...");
  }
}

Animal Interface in Different Packages

In this second example, we have the same interface called Animal but this time it resides in the com.avaldes.model package. Since our Dog Class resides in another package com.avaldes.tutorial you will notice that we must use the import statement and use the fully qualified name com.avaldes.model.Animal.

package com.avaldes.model;

public interface Animal {
  public void eat();
  public void speak();
  public void sleep();
}

Dog Class in Different Packages

package com.avaldes.tutorial;

import com.avaldes.model.Animal;

public class Dog implements Animal {
  public void eat() {
    System.out.println("I love bones...");
  }

  public void speak() {
    System.out.println("Woof, Woof.");
  }

  public void sleep() {
    System.out.println("Sleeping...");
  }
}

Importing All Classes from Java Package

Sometime it makes sense to use the wildcard (*) designation to import all of the the classes from a particular package. This will eliminate the need to import each of the classes individually and save you quite a bit of time and effort.

Please Note: If you are using Eclipse or Eclipse-based IDE like I am (I use STS Spring Tool Suite), you can use Ctrl + Shift + O to organize imports and automatically add imports as needed.

import com.avaldes.model.*;

Using Fully Qualified Name to use Classes from Another Package

We can use a class from another package without having to use the import statement. This involves using the fully qualified class name when we use the class. By doing this, the compiler will know exactly where class resides and in what package removing all ambiguity.

package com.avaldes.model;

public interface Animal {
  public void eat();
  public void speak();
  public void sleep();
}

Dog Class using Fully Qualified Name

package com.avaldes.tutorial;

public class Dog implements com.avaldes.model.Animal {
  public void eat() {
    System.out.println("I love bones...");
  }

  public void speak() {
    System.out.println("Woof, Woof.");
  }

  public void sleep() {
    System.out.println("Sleeping...");
  }
}
java_package

Core Java Related Tutorials

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *