Java Packages
Packages in Java are a mechanism that encapsulates a group of
classes, sub-packages, and interfaces. Packages are used for:
Prevent naming conflicts by allowing classes with the same
name to exist in different packages,
like college.staff.cse.Employee and college.staff.ee.Employ
ee.
They make it easier to organize, locate, and use classes,
interfaces, and other components.
Packages also provide controlled access for Protected members
that are accessible within the same package and by subclasses.
Also for default members (no access specifier) that are
accessible only within the same package.
By grouping related classes into packages, Java promotes data
encapsulation, making code reusable and easier to manage. Simply
import the desired class from a package to use it in your program.
Working of Java Packages
Directory Structure: Package names and directory structures are
closely related. For example, if a package name is college.staff.cse,
then three directories are, college, staff, and cse, where cse is
inside staff, and staff is inside the college.
Naming Conventions: Package names are written in reverse order of
domain names, e.g., org.geeksforgeeks.practice. In a college, the
convention might be:
college.tech.cse
college.tech.ee
college.art.history
Example:
import java.util.*;
Here, util is a sub-package created inside the java package.
Accessing Classes Inside a Package
In Java, we can import classes from a package using either of the
following methods:
1. Import a specific class:
import java.util.Vector;
This imports only the Vector class from the java.util package.
2. Import all classes from a package:
import java.util.*;
This imports all classes and interfaces from the java.util package but
does not include sub-packages.
Example: Importing Class
// Import the Vector class from
// the java.util package
import java.util.Vector;
public class Geeks {
public Geeks() {
// java.util.Vector is imported, hence we are
// able to access it directly in our code.
Vector v = new Vector();
// java.util.ArrayList is not imported, hence
// we are referring to it using the complete
// package name.
java.util.ArrayList l = new java.util.ArrayList();
}
public static void main(String[] args) {
// Creating an instance of Geeks
// class to invoke the constructor
new Geeks();
Note:
Using import package.*; imports all classes in a package, but not
classes from its sub-packages.
When two packages have classes with the same name (e.g.,
java.util.Date and my.package.Date), use the fully qualified name
to avoid conflicts:
import java.util.Date;
import my.package.Date;
Types of Java Packages
Built-in Packages
User-defined Packages
1. Built-in Packages
These packages consist of a large number of classes which are a part
of Java API.Some of the commonly used built-in packages are:
java.lang: Contains language support classes(e.g classes which
defines primitive data types, math operations). This package is
automatically imported.
java.io: Contains classes for supporting input / output
operations.
java.util: Contains utility classes which implement data
structures like Linked List, Dictionary and support ; for Date /
Time operations.
java.applet: Contains classes for creating Applets.
java.awt: Contain classes for implementing the components for
graphical user interfaces (like button , ;menus etc). 6)
java.net: Contain classes for supporting networking operations.
2. User-defined Packages
These are the packages that are defined by the user.
1. Create the Package:
First we create a directory myPackage (name should be same as the
name of the package). Then create the MyClass inside the directory
with the first statement being the package names.
// Name of the package must be same as the directory
// under which this file is saved
package myPackage;
public class MyClass
public void getNames(String s)
System.out.println(s);
2. Use the Class in Program:
Now we will use the MyClass class in our program.
// import 'MyClass' class from 'names' myPackage
import myPackage.MyClass;
public class Geeks {
public static void main(String args[]) {
// Initializing the String variable
// with a value
String s = "GeeksforGeeks";
// Creating an instance of class MyClass in
// the package.
MyClass o = new MyClass();
o.getNames(s);
Note: MyClass.java must be saved inside the myPackage directory
since it is a part of the package.