Understanding Java Packages: A Comprehensive Guide with Examples

Understanding Java Packages: A Comprehensive Guide with Examples Photo
Author: Tatyana Milkina
  1. What is a Package?
  2. Adding a Class to a Package
  3. Naming Rules for Packages
  4. Compiling and Running Package Files
  5. Example of a Multi-Level Package
  6. Importing Packages
  7. Key Points

1. What is a Package?

In Java, projects often include numerous classes, making it inconvenient to keep them all in one directory. Additionally, conflicts may arise if two programmers create classes with the same name. Java offers a solution with packages. Packages act like directories in the file system and must align with it.

2. Adding a Class to a Package

How do you add a class to a package? There are two main steps:

  • First, use the package keyword as the first line in your file to specify the package name.
  • Second, ensure the class resides in a directory named after the package.

Here’s an example of adding the MyFirstApp class to the lesson1 package:

package lesson1;

public class MyFirstApp {
    public static void main(String[] args) throws Exception {
        System.out.print("Hello world!!!");
    }
}

3. Naming Rules for Packages

There are specific naming rules for packages. For commercial projects, start the package name with com, followed by the organization name and project name. Functional descriptors usually follow this hierarchy. For example, a project from examclouds.com could have packages like com.examclouds.examples.code.lesson1. Use lowercase letters for package names.

Packages not only organize classes but also control access. Classes within a package can share detailed information while restricting it from external code.

4. Compiling and Running Package Files

To compile a package file, consider the following project structure: a root directory project1, with src and classes subdirectories. Place the MyFirstApp.java file in the lesson1 package inside src.

cd project1/src
javac -d ../classes lesson1/MyFirstApp.java

After compilation, the class file is stored in classes/lesson1. To run the program:

cd project1/classes 
java lesson1.MyFirstApp

5. Example of a Multi-Level Package

Here’s an example of a multi-level package com.company.lesson1. The class will be located in the directory com/company/lesson1:

package com.company.lesson1;

public class MyFirstApp {
    public static void main(String[] args) throws Exception {
        System.out.print("Hello world!!!");
    }
}

6. Importing Packages

To access a class from another package, use the import statement. For instance, a class first.Example1 can import second.Example2 as follows:

package first;

import second.Example2;

public class Example1 {
    public static void main(String[] args) {
        Example2 example2 = new Example2();
        System.out.print("Done!");
    }
}

7. Key Points

  1. Use the package keyword to declare a package, always as the first line in the file.
  2. If no package keyword is present, the classes are added to the default unnamed package.
  3. A class must reside in a directory matching its package name.
  4. Create package hierarchies using dots as separators.
  5. A package can contain multiple classes.
  6. A class’s full name includes its package name.
  7. Packages help manage object accessibility.
  8. The import statements follow the package declaration but precede class definitions.
  9. Classes in the java.lang package are implicitly imported.
Курс 'Java для начинающих' на Udemy Курс 'Java для начинающих' на Udemy
Read also:
Comments