Programmers can create their own packages to organise their classes and share them between programs.
Creating a Package
To create a package, write the package statement as the first statement of the source file: package mypack;. Every class in that file then belongs to the package mypack. The source file is placed in a directory with the same name as the package, mypack, and compiled. With the -d option, the compiler creates the directory automatically: javac -d . Book.java puts Book.class in ./mypack. Package names are written in lower case, and organisations often use their reversed internet domain, such as com.dpg.library, which gives the directories com/dpg/library.
Definition: Fully qualified name. The complete name of a class including its package, such as mypack.Book, which identifies the class uniquely.
Accessing a Package
A class in another file or package uses the package by import mypack.Book; or *import mypack.;, or by its fully qualified name, mypack.Book. Only public classes, and their public members, can be used from outside the package. The compiler and the JVM find packages through the CLASSPATH**, a list of directories and JAR files, which by default is the current directory. A program using mypack is compiled and run from the directory that contains the mypack folder.
Adding a Class to a Package
To add a new class to an existing package, write the new class in its own file with the same package statement, package mypack;, and compile it into the same directory. Since each file can have only one public class, a package with several public classes needs several files, all starting with the same package statement.
Access Protection
Java has four levels of access, which work together with packages. private: visible only within the class. default (no modifier): visible within the same package. protected: visible within the same package and in subclasses in other packages. public: visible everywhere. Thus default access is also called package-private.
Table 4.2: Access protection
| Access from | private | default | protected | public |
|---|---|---|---|---|
| Same class | Yes | Yes | Yes | Yes |
| Same package | No | Yes | Yes | Yes |
| Subclass, other package | No | No | Yes | Yes |
| Other package, not subclass | No | No | No | Yes |
Example 4.2: A Book package
Problem: Create a package library with a public class Book that stores a title, author and price and displays them. Import it in another class and display a book.
Solution:
- In library/Book.java, write package library; and the public class Book with a constructor and display().
- Compile with javac -d . Book.java.
- In LibraryApp.java, write import library.Book; and create a Book.
- Compile and run LibraryApp from the parent directory.
Result: The book details are printed by a class from the library package.
// File: library/Book.java
package library;
public class Book {
private String title, author;
private double price;
public Book(String t, String a, double p) {
title = t; author = a; price = p;
}
public void display() {
System.out.println(title + " by " + author + ", Rs " + price);
}
}
// File: LibraryApp.java
import library.Book;
public class LibraryApp {
public static void main(String[] args) {
Book b = new Book("Java Primer", "E. Balagurusamy", 650);
b.display();
}
}
// Commands: javac -d . library/Book.java
// javac LibraryApp.java
// java LibraryApp
// Output: Java Primer by E. Balagurusamy, Rs 650.0
Common mistakes
- Putting the package statement anywhere except the first line.
- Leaving a class or its methods non-public and then trying to use them from another package.
- Running the program from the wrong directory, so that the package cannot be found.
Key takeaways
- package name; must be the first statement in the file.
- The directory structure must match the package name.
- Only public classes and members are usable from other packages.
- protected allows access by subclasses in other packages.
Practice questions
- Where must the package statement appear?
- What does javac -d . do?
- Which access level allows only classes in the same package?
Answers to the practice questions
- As the first statement of the source file.
- It compiles the class and creates the package directories in the current directory.
- Default (no modifier).
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit IV
