Created: 2023-08-27 10:55
Status: #concept
Subject: Programming
Tags: Java java.lang java.util Directory
Java Package
They serve as Directory namespaces which contain our class definitions to organize our classes into logical parts.
- we can
import
package classes in other packages. - if we use
import static
, we can access class methods without using theClass.
as a prefix.
You can create a new package in NetBeans by right-clicking on the Source Packages section (which contains the project's packages), and then selecting New -> Java Package....
Common Main Packages
- java.lang - the defaut primitive data type wrappers are found here and are imported by default.
- java.util - has various utilities for input scanning & data structure representation.
- java.nio.file - has the
Paths
class for reading files. - java.time - contains the java.time.LocalDate class for handling date & time values.
Importing Packages with import
Once we declare a
package company.scope;
at the start of our .java
file, we can import classes from other packages.
import <pkg_name>[.<sub_pkg_name>]*.<class_name>;
// or we can import all Classes from a sub_pkg with *
import <pkg_name>[.<sub_pkg_name>]*.*;
Given this Filesystem Directory structure inside Project/src/main/java
:
shipping/
- domain/
- Company.class
- Vehicle.class
- Truck.class
- gui/
- reportscreens/
- Screen.class
- Dashboard.class
- reports/
import java.util.List;
import java.io.*;
import shipping.gui.reportscreens.*; // this will import the Screen & Dashboard classes