![]() |
VOOZH | about |
A package in Java is a mechanism to group related classes, interfaces, and sub-packages into a single unit. Packages help organize large applications, avoid naming conflicts, provide access protection, and make code modular and maintainable.
Built-in Packages comprise a large number of classes that are part of the Java API. Some of the commonly used built-in packages are:
Example: Using java.util.Random (Built-in Package)
Random number: 59
User-defined Packages are the packages that are defined by the user.
Example:
To use it in another class:
In Java, we can import classes from a package using either of the following methods:
import java.util.Vector;
This imports only the Vector class from the java.util package.
import java.util.*;
This imports all classes and interfaces from the java.util package but does not include sub-packages.
Example: Import the Vector class
[3, 5, 7]
Packages directly influence Java access levels: