CODE STRUCTURE
Java packages and APIs
A package groups related Java classes much like a folder groups files. Packages organize programs, control access and prevent conflicts between identical class names.
What are packages for?
- Related classes stay in one logical location.
- Classes in different packages can share the same name.
- Package access hides internal program details.
- Large projects become easier to maintain and extend.
Java packages include built-in Java API packages and programmer-defined packages.
What is the Java API?
The Java API is a library of ready-made classes and interfaces supplied with the Java platform. It provides tools for working with
| Package | Purpose | Example class |
|---|---|---|
java.lang | Core language types and system classes | String, Math |
java.util | Collections and utilities | ArrayList, Random |
java.time | Dates and time | LocalDate |
java.io | Data input and output | File |
Importing classes
import statement comes after the package declaration and before the class:
import paketas.Klase; // Import one class
import paketas.*; // Import classes from the packageThese two lines are general import templates. They explain syntax and do not execute anything themselves.
importtells Java which class from another package we want to use.- The form
paketas.Klasespecifies one particular class. - The asterisk
*allows the use of all classes directly within that package. - An import must precede the class declaration.
- Importing alone produces no program output: output comes from using the imported class.
import java.util.* includes java.util classes, but not, for example, classes from the java.util.concurrent package.
Using a built-in class
ArrayList is a resizable list from the java.util package:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList pirkiniai = new ArrayList<>();
pirkiniai.add("Duona");
pirkiniai.add("Pienas");
System.out.println(pirkiniai);
}
}[Duona, Pienas]
import java.util.ArrayListmakes the classArrayListavailable in this file.- An empty string list is created:
pirkiniai. - The method
add()first adds bread, then milk. println()displays the entire list in square brackets, preserving element order.- Unlike a regular array, an
ArrayListcan grow as new elements are added.
Another API package can provide entirely different functionality, such as working with dates:
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate gimtadienis = LocalDate.of(2026, 8, 15);
System.out.println("Gimtadienis: " + gimtadienis);
}
}Gimtadienis: 2026-08-15
LocalDateclass is imported from thejava.timepackage.- The method
of()receives the year2026, month8and day15. - The created date object is stored in the variable
gimtadienis. - Java displays the date in the standard format
metai-mėnuo-diena, so we see 2026-08-15.
java.lang is imported automatically
String, System, Math and other java.lang classes can be used without a separate import. Java makes this package available automatically.
Creating your own package
package declaration must be the first program statement in the file:
// File: src/lt/pavyzdys/zmones/Asmuo.java
package lt.pavyzdys.zmones;
public class Asmuo {
public String vardas = "Ona";
}This file only defines the class Asmuo. It has no main() or println(), so the class itself prints nothing.
- The file
Asmuo.javais stored in folders matching the packagelt.pavyzdys.zmones. - The first program statement declares which package the class belongs to.
public class Asmuocreates a public class usable from other packages.- The class has a public text field,
vardas, storing the value Ona.
Package names usually use lowercase, often a reversed domain name such as lt.pavyzdys.zmones. The folder structure must match the package components.
Using a class from another package
A public class in another package must be imported:
// File: src/lt/pavyzdys/app/Main.java
package lt.pavyzdys.app;
import lt.pavyzdys.zmones.Asmuo;
public class Main {
public static void main(String[] args) {
Asmuo asmuo = new Asmuo();
System.out.println(asmuo.vardas);
}
}Ona
- Class
Mainbelongs to a different package,lt.pavyzdys.app. import lt.pavyzdys.zmones.Asmuospecifies the exact location of the desired class.- With
new Asmuo()creates a new instance of this class. - Through
asmuo.vardasaccesses the value stored in the class, Ona. - For this to work, the class and the field used from another package must be accessible, in this case marked
public.
A fully qualified class name
Instead of importing, you can write the full package and class name each time:
public class Main {
public static void main(String[] args) {
java.time.LocalDate data = java.time.LocalDate.of(2026, 8, 15);
System.out.println(data);
}
}2026-08-15
- This file has no separate
import java.time.LocalDateline. - Instead of the short name, the full name is written each time:
java.time.LocalDate. - Java knows exactly which
LocalDateclass to use. - The result is unchanged: the console still displays the date 2026-08-15.
This is useful when two classes share the same short name. In ordinary code, explicit individual imports are usually easier to read.
Guidelines for using packages
- Group classes by responsibility or application domain.
- Use short, lowercase package names.
- Expose only the classes and members needed by other packages.
- Prefer explicit class imports when they make dependencies clearer.
- When using an API class, check the official documentation for your Java version.