Java Packages Overview
Java Packages Overview
Chapters Categories
Java - Packages
Java Packages
Packages are used in Java in order to prevent naming conflicts, control access, make
searching/locating and usage of classes, interfaces, enumerations, and annotations
easier, etc.
Since the package creates a new namespace there won't be any name conflicts with
names in other packages. Using packages, it is easier to provide access control and it is
Page 2 of 11
The package statement should be the first line in the source file. There can be only one
package statement in each source file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and
annotation types will be placed in the current default package.
Then a folder with the given package name is created in the specified destination, and
the compiled class files will be placed in that folder.
interface Animal {
public void eat();
public void travel();
}
Now, let us implement the above interface in the same package animals −
Page 3 of 11
Open Compiler
package animals;
/* File name : MammalInt.java */
interface Animal {
public void eat();
public void travel();
}
Output
$ javac -d . Animal.java
$ javac -d . MammalInt.java
Now a package/folder with the name animals will be created in the current directory
and these class files will be placed in it as shown below.
Page 4 of 11
You can execute the class file within the package and get the result as shown below.
Mammal eats
Mammal travels
Example
Here, a class named Boss is added to the payroll package that already contains
Employee. The Boss can then refer to the Employee class without using the payroll
prefix, as demonstrated by the following Boss class.
package payroll;
public class Boss {
public void payEmployee(Employee e) {
e.mailCheck();
}
}
What happens if the Employee class is not in the payroll package? The Boss class must
then use one of the following techniques for referring to a class in a different package.
The fully qualified name of the class can be used. For example −
Page 5 of 11
payroll.Employee
The package can be imported using the import keyword and the wild card (*). For
example −
import payroll.*;
The class itself can be imported using the import keyword. For example −
import payroll.Employee;
Example
package payroll;
Example
package payroll;
import payroll.Employee;
boss.payEmployee(e);
}
}
Output
Pay received.
Note − A class file can contain any number of import statements. The import statements
must appear after the package statement and before the class declaration.
The name of the package becomes a part of the name of the class, as we just
discussed in the previous section.
The name of the package must match the directory structure where the
corresponding bytecode resides.
Put the source code for a class, interface, enumeration, or annotation type in a text file
whose name is the simple name of the type and whose extension is .java.
For example −
Now, put the source file in a directory whose name reflects the name of the package to
which the class belongs −
....\vehicle\Car.java
In general, a company uses its reversed Internet domain name for its package names.
Example − A company's Internet domain name is apple.com, then all its package
names would start with com.apple. Each component of the package name corresponds to
a subdirectory.
....\com\apple\computers\Dell.java
At the time of compilation, the compiler creates a different output file for each class,
interface and enumeration defined in it. The base name of the output file is the name of
the type, and its extension is .class.
For example −
class Ups {
}
$javac -d . Dell.java
.\com\apple\computers\Dell.class
.\com\apple\computers\Ups.class
You can import all the classes or interfaces defined in \com\apple\computers\ as follows
−
Page 8 of 11
import com.apple.computers.*;
Like the .java source files, the compiled .class files should be in a series of directories
that reflect the package name. However, the path to the .class files does not have to be
the same as the path to the .java source files. You can arrange your source and class
directories separately, as −
<path-one>\sources\com\apple\computers\Dell.java
<path-two>\classes\com\apple\computers\Dell.class
By doing this, it is possible to give access to the classes directory to other programmers
without revealing your sources. You also need to manage source and class files in this
manner so that the compiler and the Java Virtual Machine (JVM) can find all the types
your program uses.
The full path to the classes directory, <path-two>\classes, is called the class path, and is
set with the CLASSPATH system variable. Both the compiler and the JVM construct the
path to your .class files by adding the package name to the class path.
A class path may include several paths. Multiple paths should be separated by a
semicolon (Windows) or colon (Unix). By default, the compiler and the JVM search the
current directory and the JAR file containing the Java platform classes so that these
directories are automatically in the class path.
TOP TUTORIALS
Python Tutorial
Java Tutorial
C++ Tutorial
C Programming Tutorial
C# Tutorial
PHP Tutorial
R Tutorial
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
SQL Tutorial
TRENDING TECHNOLOGIES
Docker Tutorial
Kubernetes Tutorial
DSA Tutorial
CERTIFICATIONS
Online Go Compiler
Online C Compiler
Online C++ Compiler
Online C# Compiler
Online PHP Compiler
Online MATLAB Compiler
Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.