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 , , files, networking, input and many other tasks.

Common Java API packages
PackagePurposeExample class
java.langCore language types and system classesString, Math
java.utilCollections and utilitiesArrayList, Random
java.timeDates and timeLocalDate
java.ioData input and outputFile

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 package
There is no console output.

These two lines are general import templates. They explain syntax and do not execute anything themselves.

  1. import tells Java which class from another package we want to use.
  2. The form paketas.Klase specifies one particular class.
  3. The asterisk * allows the use of all classes directly within that package.
  4. An import must precede the class declaration.
  5. Importing alone produces no program output: output comes from using the imported class.
The asterisk does not import subpackages.

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);
    }
}
Console output

[Duona, Pienas]

  1. import java.util.ArrayList makes the class ArrayList available in this file.
  2. An empty string list is created: pirkiniai.
  3. The method add() first adds bread, then milk.
  4. println() displays the entire list in square brackets, preserving element order.
  5. Unlike a regular array, an ArrayList can 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);
    }
}
Console output

Gimtadienis: 2026-08-15

  1. LocalDate class is imported from the java.time package.
  2. The method of() receives the year 2026, month 8 and day 15.
  3. The created date object is stored in the variable gimtadienis.
  4. 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";
}
There is no console output.

This file only defines the class Asmuo. It has no main() or println(), so the class itself prints nothing.

  1. The file Asmuo.java is stored in folders matching the package lt.pavyzdys.zmones.
  2. The first program statement declares which package the class belongs to.
  3. public class Asmuo creates a public class usable from other packages.
  4. 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);
    }
}
Console output

Ona

  1. Class Main belongs to a different package, lt.pavyzdys.app.
  2. import lt.pavyzdys.zmones.Asmuo specifies the exact location of the desired class.
  3. With new Asmuo() creates a new instance of this class.
  4. Through asmuo.vardas accesses the value stored in the class, Ona.
  5. 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);
    }
}
Console output

2026-08-15

  1. This file has no separate import java.time.LocalDate line.
  2. Instead of the short name, the full name is written each time: java.time.LocalDate.
  3. Java knows exactly which LocalDate class to use.
  4. 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.