Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Java program architecture

Java program architecture: Java architecture uses package and JPMS module boundaries: module-info.java declares requires, exports, opens, uses, and provides, while the JVM loads compiled bytecode. A fluent builder with a recursive self type, such as B extends Builder<B>, preserves the concrete builder type across chained methods and inheritance.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

architecture

Java architecture uses package and JPMS module boundaries: module-info.java declares requires, exports, opens, uses, and provides, while the JVM loads compiled bytecode. A fluent builder with a recursive self type, such as B extends Builder, preserves the concrete builder type across chained methods and inheritance.

Expose an interface from a separate module and bind implementations through ServiceLoader so consumers depend on the contract rather than an implementation package. Keep a DTO and domain model as separate types joined by explicit mapping; dependency injection exposes required collaborators most clearly through constructor injection.

exports makes public types accessible for compilation but does not open private members to deep reflection; missing opens can produce InaccessibleObjectException. Field injection hides object requirements and complicates tests; a per-container singleton means one instance in that container, not one global JVM singleton.

A requires cycle is rejected, as is a module reading two modules that export the same package to it; an automatic module weakens boundary control and name stability. A cyclic dependency prevents a pure constructor-injection graph from being built; break the cycle by redesigning responsibilities, using a factory, or a controlled indirection.

The program reads the first four bytes of its own Main.class and prints CAFEBABE, demonstrating the class-file signature of the artifact loaded by the JVM.

Examples

import java.io.InputStream;

public class Main {
    public static void main(String[] args) throws Exception {
        try (InputStream bytecode = Main.class.getResourceAsStream("Main.class")) {
            byte[] magic = bytecode.readNBytes(4);
            System.out.printf("%02X%02X%02X%02X%n", magic[0], magic[1], magic[2], magic[3]);
        }
    }
}

Result

CAFEBABE

The program reads the first four bytes of its own Main.class and prints CAFEBABE, demonstrating the class-file signature of the artifact loaded by the JVM.

Sources