Understanding Java Packages: Creation, Usage, and Classpath Management
This article explains how Java packages are created with the package statement, how they organize classes into namespaces, how to import and use them in code, and how to configure the classpath for compilation and execution, providing practical examples and code snippets.
Package Creation
In a Java project, multiple .java files are organized using packages. Adding a package declaration at the beginning of a source file places the class into that package. For example, the following Human class is placed in the com.vamei.society package:
package com.vamei.society;
public class Human {
/**
* constructor
*/
public Human(int h) {
this.height = h;
System.out.println("I'm born");
}
/**
* accessor
*/
public int getHeight() {
return this.height;
}
/**
* mutator
*/
public void growHeight(int h) {
this.height = this.height + h;
}
private int height;
}The first line package com.vamei.society; indicates that the class belongs to the com.vamei.society package, where com.vamei is a reversed domain name used as a unique prefix.
Package Usage
After compiling, the Human.class file should be placed in the corresponding directory structure com/vamei/society/. Other classes can access it using an import statement:
import com.vamei.society.*;
public class TestAgain {
public static void main(String[] args) {
Human aPerson = new Human(180);
System.out.println(aPerson.getHeight());
}
}The asterisk (*) imports all classes in the society package. Alternatively, a class can be referenced by its fully‑qualified name without an import:
public class TestAgain {
public static void main(String[] args) {
com.vamei.society.Human aPerson = new com.vamei.society.Human(180);
System.out.println(aPerson.getHeight());
}
}Classpath Configuration
If the package directory is not on the current working path, the Java compiler and runtime need to be told where to find it using the -classpath option or the CLASSPATH environment variable. For example:
$javac -classpath /home/vamei/javapackage:. TestAgain.java
$java -classpath /home/vamei/javapackage:. TestAgainThis tells Java to search both the specified package directory and the current directory for classes.
Additional Notes
Java packages provide a namespace, allowing classes with the same name to coexist in different packages (e.g., com.vamei.society.Human vs. com.vamei.creature.Human). Classes without an explicit access modifier have default (package‑private) visibility, meaning they are accessible only within the same package.
The package mechanism is similar to import systems in other languages such as Python, and it contributes to Java’s “write once, run anywhere” promise by enabling portable .class files that run on any JVM.
Summary
Key points: package and import statements, default (package‑private) access, using fully‑qualified class names, configuring the classpath with -classpath or CLASSPATH, and the role of packages in Java’s portability.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
