Understanding Java Packages: A Comprehensive Guide with Examples
- What is a Package?
- Adding a Class to a Package
- Naming Rules for Packages
- Compiling and Running Package Files
- Example of a Multi-Level Package
- Importing Packages
- 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
- Use the package keyword to declare a package, always as the first line in the file.
- If no package keyword is present, the classes are added to the default unnamed package.
- A class must reside in a directory matching its package name.
- Create package hierarchies using dots as separators.
- A package can contain multiple classes.
- A class’s full name includes its package name.
- Packages help manage object accessibility.
- The import statements follow the package declaration but precede class definitions.
- Classes in the
java.lang
package are implicitly imported.
Please log in or register to have a possibility to add comment.