Packages in Java
What is a Package in Java?
Java packages is a technique of bundling classes, interfaces, and other packages in Java to group these together as classes, interfaces, and sub-packages. This is how you can group or bundle your classes under namespaces as an efficient approach to managing big systems. This is the logical structure of a package as a package can include classes, and interfaces and still can include other packages. It is useful in avoiding name collision because each class gets its own space in the package structure.
In essence, packages serve several purposes:
- Namespace management: A package also looks for the names of classes in large applications to minimize the likelihood of having two classes with the same name in different packages.
- Access control: As a result of packages one can be able to set access levels of classes and interfaces. With the help of such keywords as public, protected, private, and without any, you can determine the access within a package.
- Code organization: A package gives you a way of grouping classes that belong to the same functionality and makes your program easier to manage.
In Java, a package will be declared at the start of a Java source code using the keyword package.
package com.example.util;
public class MyUtility {
// Class code here
}
Here, the MyUtility
class is part of the com.example.util
package.
Types of Packages in Java
There are two types of packages in Java:
✅Built-in Java Packages: It should also be pointed out that Java has a great amount of built-in packages with ready-made classes and interfaces. These Java packages are in the Java Standard Library (JDK) also known as Java Development Kit and it consists of packages for Input /Output, Networking, Data structures, GUI, and many others. Some popular built-in packages include:
java.lang
: This package is brought in by default in each and every Java program. Among them, there exist primitive classes like String, Object, Math, Thread System, etc.java.util
: This one also offers helpful classes; for instance, ArrayList, HashMap, Date, and Collections.java.io
: It holds classes for input and output purposes like File, BufferedReader, PrintWriter, and so on.java.net
: Offers classes for network interfaces including URL, Socket, InetAddress, etcjava.awt
andjavax.swing
: These java packages offer classes for creating graphical user interfaces (GUIs).
Example of using a built-in java package:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Programming");
System.out.println(list);
}
}
✅User-defined Java Packages: The language also has other packages that are already integrated into Java; a special feature of Java is that the developer can define their own packages. These are known as user-defined packages. With its help you can sort the code by logical groups, avoid naming problems, as well as have better reusability. A user-defined package can be developed using the package keyword in which the user assigns the package name to the classes contained in directories that reflect the package structure.
Example of a user-defined Java package:
package com.mydomain.myapp;
public class MyClass {
public void display() {
System.out.println("Hello from MyClass in myapp package.");
}
}
In this case, the class MyClass is put into the directory hierarchy corresponding to the package com.mydomain.myapp.
How to Create and Use Packages in Java
✅Creating a Java Package
To create a package in Java, follow these steps:
- The organization of the project directory should mirror the name used for the package. For instance, should your package name be com.example.utility, ensure you have a directory tree like com/example/utility.
- This is where your Java classes should be placed by you.
- Java also allows you to structure your work using the package keyword at the top of your files to determine the package name.
Here’s an example:
mkdir -p com/example/utility
Inside com/example/utility/
, create a Java file called MyUtility.java
:
package com.example.utility;
public class MyUtility {
public static void displayMessage(String message) {
System.out.println(message);
}
}
✅Compiling the Java Package
To compile classes in the Java package, move up one directory level to ‘com’ and then use the javac command. This command compiles MyUtility.java and the compiled bytecode will be located in the said directories.
For example, if your directory structure is /home/user/myproject/com/example/utility/
, you would run the following command:
javac com/example/utility/MyUtility.java
✅Using a Java Package
To use the class from a Java package in the program you have to have it imported or used in some way. There are two ways to import classes:
Single class import: Use import to bring a particular class from the Java package.
import com.example.utility.MyUtility;
public class Main {
public static void main(String[] args) {
MyUtility.displayMessage("Hello from MyUtility");
}
}
Wildcard import: Import with a wildcard * to import all classes from the Java package; otherwise, import one class at a time as seen from the given examples.
import com.example.utility.*;
public class Main {
public static void main(String[] args) {
MyUtility.displayMessage("Hello from MyUtility");
}
}
✅ Running the Program
To run the program, you will type java and then the fully qualified name of the class that contains the main () method. For example:
java com.example.utility.Main
Conclusion
More specifically, Java packages in coding languages such as Java represent an important asset that may be of much help to engineers whereby they can make an effective code structure, keep large codes in order, and prevent naming conventions. One can organize Java code reasonably well by using packages, also it is possible to use a number of packages implemented in Java.