Writes, reviews and modernizes Java in Android apps: Java 17 and 21 features that D8 supports, immutability, JSpecify nullability for Kotlin interop, leak-free threading without static Context or inner-class Handlers, ExecutorService, LiveData, ViewBinding, ListAdapter and DiffUtil, tests with JUnit, Mockito and Espresso, and incremental migration to Kotlin. Use when editing, reviewing or migrating .java files in an Android project.
---
name: mobile-java
description: "Writes, reviews and modernizes Java in Android apps: Java 17 and 21 features that D8 supports, immutability, JSpecify nullability for Kotlin interop, leak-free threading without static Context or inner-class Handlers, ExecutorService, LiveData, ViewBinding, ListAdapter and DiffUtil, tests with JUnit, Mockito and Espresso, and incremental migration to Kotlin. Use when editing, reviewing or migrating .java files in an Android project."
license: MIT
metadata:
author: Ahmad Hussain
version: "1.0.1"
pack: mobile-dev-kit
---
# Java on Android
Keep Java Android code correct, leak-free and Kotlin-friendly while it lives, and move it to Kotlin in
small safe steps when the team decides to.
## When to use
- Editing, fixing or reviewing `.java` files in an Android app or library.
- Bumping the Java language level, or using a JDK API that may not exist on the app's `minSdk`.
- Java code that Kotlin code calls (or will call) and needs nullability and interop annotations.
- Leaks, ANRs or crashes caused by Handlers, executors, Fragments, adapters or unclosed resources.
- Planning or executing an incremental Java-to-Kotlin migration.
Not for: Kotlin-first code (use `mobile-kotlin`), architecture choices (use `mobile-mvvm`, `mobile-mvi`).
## Workflow
1. Read the module's build file: `compileOptions` source and target level, `minSdk`, and whether
`isCoreLibraryDesugaringEnabled` is on. These decide which features and APIs are safe.
2. Load the reference that matches the task: [Threading](references/threading.md),
[Migration to Kotlin](references/migration-to-kotlin.md), [Testing](references/testing.md).
3. If the project already builds Kotlin and the team has not said otherwise, write new classes in Kotlin.
Change existing Java minimally and in place.
4. Add nullability annotations to every public signature you touch.
5. Apply the rules below. Prefer fixing leaks and threading over stylistic rewrites.
6. Run lint (`./gradlew lint`) for `NewApi`, threading annotation and resource-leak checks, then the tests.
7. Keep conversion or modernization commits separate from behaviour changes.
## Toolchain: what Java you can use
| Feature | Java | On Android |
|---|---|---|
| Lambdas, method references, default and static interface methods | 8 | Desugared by D8 for every API level |
| `var`, switch expressions, text blocks, `instanceof` patterns | 10-16 | Compile-time features, every API level |
| Records | 16 | Supported by current AGP (D8 desugars them). Check reflection-based JSON libraries |
| Sealed classes and interfaces | 17 | Only on recent AGP / D8. Verify before relying on them |
| Pattern matching for `switch`, record patterns | 21 | Needs Java 21 source level and AGP / D8 support. Verify first |
| `java.time` (API 26), `java.util.stream`, `Optional`, `java.util.function` (API 24) | library | Below those API levels only with core library desugaring |
| Newer JDK methods (`List.of`, `List.getFirst()`, `Stream.toList()`) | library | Tied to newer API levels. Trust lint `NewApi`; desugaring covers only part |
The JDK that runs Gradle (17+ for current AGP) is separate from the language level and from the APIs
available at runtime. The runtime APIs come from the device's Android version.
## Rules
### 1. Language level is free, library APIs are not
```java
// BAD: compiles against a recent compileSdk, crashes on older devices (added to Android in API 35)
Device first = devices.getFirst();
// GOOD
Device first = devices.get(0);
```
```kotlin
// GOOD: java.time on minSdk below 26 through core library desugaring (app/build.gradle.kts)
android {
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
dependencies { coreLibraryDesugaring(libs.desugar.jdk.libs) }
```
Never suppress `NewApi` without a `Build.VERSION.SDK_INT` check or confirmed desugaring.
### 2. Immutable by default
```java
// BAD: public mutable fields, internal list shared with callers
public class Device {
public String name;
public List<String> tags;
}
// GOOD: record with a defensive copy (works on every API level)
public record Device(String id, String name, List<String> tags) {
public Device {
Objects.requireNonNull(id);
tags = Collections.unmodifiableList(new ArrayList<>(tags));
}
}
```
- No records? `final` class, `private final` fields, no setters, copies in and unmodifiable views out.
- `static final` constants only for truly immutable values. A `static final List` is still mutable.
### 3. Nullability is declared for Kotlin callers
```java
// package-info.java: everything in the package is non-null unless marked
@NullMarked
package com.example.devices;
import org.jspecify.annotations.NullMarked;
```
```java
// BAD: Kotlin sees String! (platform type) and skips null checks, NPE at runtime
public String findName(String id) { return names.get(id); }
// GOOD: inside @NullMarked, only nullable values are annotated
public @Nullable String findName(String id) { return names.get(id); }
```
- JSpecify `@Nullable` is a type-use annotation: `List<@Nullable String>`, `String @Nullable []`.
- Packages not yet `@NullMarked` use AndroidX `@NonNull` / `@Nullable` on every public parameter and return.
- Recent Kotlin compilers report JSpecify violations as errors, so wrong annotations break the Kotlin build.
### 4. Nothing long-lived references an Activity, Fragment or View
```java
// BAD: static field keeps the Activity alive for the process lifetime
private static Context context;
// BAD: non-static inner class holds the outer Activity while queued or running
private class Poller implements Runnable {
@Override public void run() { statusView.setText(read()); }
}
// GOOD: application context where a Context must be stored; work moved to a ViewModel
public final class StatusRepository {
private final Context appContext;
public StatusRepository(Context context) { this.appContext = context.getApplicationContext(); }
}
```
- Anonymous classes and lambdas that use outer members capture the outer instance too.
- `WeakReference` is a last resort for legacy callbacks, not a design.
### 5. Main-thread work goes through an explicit Looper; no AsyncTask
```java
// BAD: deprecated, binds to the calling thread's Looper
Handler handler = new Handler();
// BAD: AsyncTask is deprecated (API 30); leaks the Activity and loses results on rotation
new LoadTask().execute();
// GOOD
private final Handler main = new Handler(Looper.getMainLooper());
@Override protected void onDestroy() {
main.removeCallbacksAndMessages(null);
super.onDestroy();
}
```
Replace `AsyncTask` with an `ExecutorService` in a ViewModel that publishes through `LiveData`
(see [Threading](references/threading.md)).
### 6. Every executor has an owner that shuts it down; every Future can be cancelled
```java
// BAD: new thread pool per call, never shut down
Executors.newFixedThreadPool(4).execute(this::sync);
// GOOD: owned by the ViewModel, cancelled with it
public final class DevicesViewModel extends ViewModel {
private final ExecutorService io = Executors.newSingleThreadExecutor();
private @Nullable Future<?> loading;
public void load() {
if (loading != null) loading.cancel(true);
loading = io.submit(this::loadBlocking);
}
@Override protected void onCleared() { io.shutdownNow(); }
}
```
If the executor is injected and shared app-wide, cancel the `Future`s in `onCleared()` instead of shutting
the executor down.
### 7. LiveData: right setter, read-only exposure, view lifecycle owner
```java
// BAD: setValue from a worker thread throws; mutable type exposed
public MutableLiveData<UiState> state = new MutableLiveData<>();
io.execute(() -> state.setValue(load()));
// GOOD
private final MutableLiveData<UiState> state = new MutableLiveData<>(UiState.loading());
public LiveData<UiState> getState() { return state; }
io.execute(() -> state.postValue(load()));
```
```java
// BAD (Fragment): observers pile up after each view recreation
viewModel.getState().observe(this, this::render);
// GOOD
viewModel.getState().observe(getViewLifecycleOwner(), this::render);
```
`postValue` keeps only the latest pending value. Use it for state, not for events that must all arrive.
### 8. ViewBinding is cleared in onDestroyView
```java
// BAD: binding outlives the Fragment's view and leaks the whole view tree
private FragmentDevicesBinding binding;
// GOOD
private @Nullable FragmentDevicesBinding binding;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
binding = FragmentDevicesBinding.bind(view);
}
@Override
public void onDestroyView() {
binding = null;
super.onDestroyView();
}
```
Use the `Fragment(R.layout.fragment_devices)` constructor so `onCreateView` is not needed.
### 9. Lists use ListAdapter and DiffUtil
```java
// BAD: full rebind, no animations, scroll state jumps
adapter.setItems(items);
adapter.notifyDataSetChanged();
// GOOD
public final class DeviceAdapter extends ListAdapter<Device, DeviceAdapter.Holder> {
private static final DiffUtil.ItemCallback<Device> DIFF = new DiffUtil.ItemCallback<>() {
@Override public boolean areItemsTheSame(@NonNull Device a, @NonNull Device b) { return a.id().equals(b.id()); }
@Override public boolean areContentsTheSame(@NonNull Device a, @NonNull Device b) { return a.equals(b); }
};
public DeviceAdapter() { super(DIFF); }
// onCreateViewHolder / onBindViewHolder use getItem(position)
}
adapter.submitList(items); // new list instance each time; never mutate a submitted list
```
### 10. Resources are closed with try-with-resources
```java
// BAD: leaks on exception, StrictMode reports it
InputStream in = resolver.openInputStream(uri);
byte[] data = readAll(in);
in.close();
// GOOD
try (InputStream in = resolver.openInputStream(uri)) {
if (in == null) throw new FileNotFoundException(uri.toString());
return readAll(in);
}
```
Applies to `Cursor`, streams, `ParcelFileDescriptor`, `TypedArray` (API 31+ implements `AutoCloseable`;
call `recycle()` below that).
### 11. equals and hashCode are overridden together
```java
// BAD: equals without hashCode breaks HashSet, HashMap and DiffUtil
@Override public boolean equals(Object o) { return o instanceof Device d && id.equals(d.id); }
// GOOD: both, over the same fields (or use a record)
@Override public boolean equals(@Nullable Object o) {
return o instanceof Device d && id.equals(d.id) && name.equals(d.name);
}
@Override public int hashCode() { return Objects.hash(id, name); }
```
### 12. Exceptions and interrupts are never swallowed
```java
// BAD
try { worker.join(); } catch (InterruptedException e) { }
try { sync(); } catch (Exception e) { e.printStackTrace(); }
// GOOD
try {
worker.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
try {
sync();
} catch (IOException e) {
state.postValue(UiState.offline());
}
```
### 13. Interop annotations follow the direction of calls
```kotlin
// Kotlin code called from Java
class Units {
companion object {
@JvmStatic fun celsius(f: Double): Double = (f - 32) / 1.8 // Units.celsius(..) from Java
const val MAX_MTU = 517 // compile-time constant
}
}
class ScanSpec @JvmOverloads constructor(val timeoutMs: Long = 10_000, val lowLatency: Boolean = false)
```
`@JvmField` exposes a non-`const` property as a field. Details and ordering are in
[Migration to Kotlin](references/migration-to-kotlin.md).
### 14. Tests are deterministic
```java
// BAD: real threads and sleeps
viewModel.load();
Thread.sleep(500);
// GOOD: inject a direct executor; InstantTaskExecutorRule runs LiveData synchronously
@Rule public InstantTaskExecutorRule liveData = new InstantTaskExecutorRule();
DevicesViewModel vm = new DevicesViewModel(fakeRepo, Runnable::run);
```
See [Testing](references/testing.md) for Mockito, Espresso and `IdlingResource`.
## Anti-patterns
- Rewriting a working Java class in a review that was about a bug fix.
- `static` fields holding `Context`, `Activity`, `View`, `Fragment`, adapters or listeners.
- `new Handler()`, `AsyncTask`, `Executors.new...` inside an Activity or Fragment.
- `observe(this, ...)` in a Fragment; ViewBinding fields never cleared.
- `notifyDataSetChanged()` for list updates; mutating a list already passed to `submitList`.
- Public signatures without nullability annotations in code Kotlin calls.
- `catch (Exception e) { e.printStackTrace(); }` and empty `InterruptedException` handlers.
- Raising the Java level and assuming the JDK library on the device matches.
- Converting a file to Kotlin and changing its behaviour in the same commit.
- Logging tokens, device identifiers or personal data with `Log.d` (escalate to `mobile-security`).
## Checklist
- [ ] Language level, `minSdk` and core library desugaring checked before using a JDK API
- [ ] Lint `NewApi` clean; no blanket suppressions
- [ ] Value types immutable (records or final fields, defensive copies)
- [ ] Package `@NullMarked` or public signatures annotated with `@Nullable` / `@NonNull`
- [ ] No static or long-lived references to Activity, Fragment, View or non-application Context
- [ ] Handlers use `Looper.getMainLooper()` and are cleared; no `AsyncTask` in new code
- [ ] Executors shut down by their owner; Futures cancelled in `onCleared()`
- [ ] `setValue` on main thread, `postValue` from workers; `LiveData` exposed read-only
- [ ] Fragments observe with `getViewLifecycleOwner()`; binding nulled in `onDestroyView()`
- [ ] RecyclerView uses `ListAdapter` + `DiffUtil` with id-based `areItemsTheSame`
- [ ] Closeables in try-with-resources
- [ ] `equals` and `hashCode` overridden together
- [ ] No swallowed exceptions; interrupt status restored
- [ ] Tests use direct executors and `InstantTaskExecutorRule`; Espresso uses `IdlingResource`, not sleeps
## Related skills
- `mobile-kotlin` -- target idioms when migrating, and Kotlin-side interop rules
- `mobile-mvvm` -- ViewModel and LiveData or StateFlow architecture
- `mobile-design-patterns` -- Builder, Factory and Strategy where Java needs them
- `mobile-code-review` -- review workflow and severity levels
- `mobile-security` -- storage, logging and network rules for legacy Java code
- `mobile-ci-cd` -- lint and test gates in the build pipeline
- `mobile-testing` -- cross-platform test strategy
don't have the plugin yet? install it then click "run inline in claude" again.
Keep Java Android code correct, leak-free and Kotlin-friendly while it lives, and move it to Kotlin in small safe steps when the team decides to.
This skill writes, reviews and modernizes Java code in Android apps and libraries. It covers Java language features supported by D8 desugaring (lambdas, records, sealed classes, pattern matching), immutability patterns, JSpecify nullability annotations for Kotlin interop, threading models that avoid leaks (Handlers, ExecutorService, LiveData), ViewBinding lifecycle safety, efficient list updates with ListAdapter and DiffUtil, deterministic testing with JUnit, Mockito and Espresso, and step-by-step migration to Kotlin. Use it when editing, fixing or reviewing .java files, bumping the Java language level, adding nullability for Kotlin callers, debugging leaks or ANRs in threading or adapters, or planning incremental Java-to-Kotlin conversion.
compileOptions (source and target Java version), minSdk, compileSdk, and isCoreLibraryDesugaringEnabled flag. These determine which JDK features and APIs are safe at runtime.java.time, Stream, List.of(), etc.) can be used without desugaring.references/threading.md: threading patterns, Handlers, ExecutorService, LiveData usage.references/migration-to-kotlin.md: annotation-driven Java-to-Kotlin conversion, @JvmStatic, @JvmOverloads, @JvmField.references/testing.md: JUnit, Mockito mocking, Espresso UI testing, InstantTaskExecutorRule, IdlingResource../gradlew lint for NewApi, threading annotations, resource-leak warnings.Read the module's build.gradle / build.gradle.kts. Record sourceCompatibility / targetCompatibility, minSdk, compileSdk, and the state of isCoreLibraryDesugaringEnabled. Input: build file. Output: a note of safe Java features and APIs for this module.
Identify the task type (bug fix in existing Java, new Java class, migration to Kotlin, modernization, review). Input: code context. Output: selected reference document (Threading, Migration to Kotlin, Testing, or all).
If the project already has a Kotlin codebase and no explicit team constraint, write new classes in Kotlin instead of Java. If changing existing Java, make edits minimal and in place. Input: codebase state, team direction. Output: decision to stay in Java or switch to Kotlin.
Examine the code for leaks, threading issues and ANRs before stylistic improvements. Apply rules 4 through 12 (nullability, immutability, Handlers, Executors, LiveData, ViewBinding, ListAdapter, closeable resources, equals/hashCode, exception handling). Input: .java file. Output: annotated or rewritten code with leak-free patterns.
Add or verify @Nullable and @NonNull annotations on every public method signature and field the code touches. If the package lacks @NullMarked, annotate individual parameters and returns with JSpecify @Nullable or AndroidX @NonNull. Input: public signatures. Output: nullability-annotated code ready for Kotlin callers.
Run ./gradlew lint and check for NewApi, threading annotation and resource-leak violations. Never suppress NewApi without a Build.VERSION.SDK_INT check or confirmed desugaring coverage. Input: compiled code. Output: lint report with zero blocking issues.
Run the app's test suite (JUnit + Espresso). Verify all tests pass. Input: test commands. Output: green test report.
Keep conversion or modernization commits separate from behavioural changes (bug fixes or feature additions). Do not rewrite a working Java file's logic as part of a code review if the review was triggered by a bug in that file. Input: commit history, review scope. Output: separated commits with clear intent in messages.
If the project has no core library desugaring but needs java.time or Stream below the device's minimum API: add coreLibraryDesugaring("com.android.tools:desugar_jdk_libs") to dependencies and enable isCoreLibraryDesugaringEnabled = true. Enable and retry compilation.
If the code uses List.getFirst(), List.of(), or other JDK 21+ methods on minSdk < 33: either enable core library desugaring for that library, downgrade to .get(0) or Collections.unmodifiableList(Arrays.asList(...)), or confirm desugaring coverage with lint. Never suppress NewApi without verification.
If a public Java signature lacks nullability annotations and Kotlin code calls it: add @Nullable or @NonNull from JSpecify or AndroidX. If the whole package is not @NullMarked, annotate every public parameter and return. If annotations are missing, Kotlin sees platform types (String!) and skips null checks, leading to NPE at runtime.
If the code holds a static reference to Activity, Fragment, View or non-application Context: refactor it out. Use application context (.getApplicationContext()) only if a Context must be stored long-term. Move UI state to ViewModel. This is a leak; fix before proceeding.
If the code uses AsyncTask, new Handler() without Looper.getMainLooper(), or creates executors inside Activity/Fragment: replace with an ExecutorService owned by ViewModel, publishing results through LiveData or callback. Cancel pending Futures in onCleared(). This prevents leaks and ANRs.
If a Fragment or Activity uses LiveData.observe(this, ...) instead of observe(getViewLifecycleOwner(), ...): change to getViewLifecycleOwner(). Observers attached to this pile up after each view recreation and leak memory.
If ViewBinding is held beyond onDestroyView(): null it out in onDestroyView() before calling super.onDestroyView(). Keeping a reference leaks the entire view tree.
If RecyclerView updates use notifyDataSetChanged() or mutate a list after submitList(): switch to ListAdapter with DiffUtil.ItemCallback. Call submitList(newList) with a fresh list instance; never mutate the old one.
If exceptions are caught and swallowed (empty catch blocks, e.printStackTrace(), or InterruptedException ignored): decide the right recovery path. For InterruptedException, restore the interrupt flag (Thread.currentThread().interrupt()) and return. For IO or parse exceptions, post an error state through LiveData or callback. Never silently drop exceptions.
If the code is deterministic test code and uses Thread.sleep() or real executors: inject a direct executor (Runnable::run) or use InstantTaskExecutorRule for LiveData. Avoid timing-dependent assertions.
If migrating Java to Kotlin incrementally and the two exist side by side: use @JvmStatic, @JvmOverloads, @JvmField to expose Kotlin public API in a way Java can call. Document the direction of calls (Java-to-Kotlin or Kotlin-to-Java) so annotations land in the right language.
The enriched Java code (or review comments) must satisfy:
sourceCompatibility = Java 21 and AGP/D8 support; no List.getFirst() on minSdk < 33 without desugaring confirmed by lint.@Nullable or @NonNull (JSpecify or AndroidX), or the package is @NullMarked.onCleared(). All Futures cancelled in onCleared(). All Closeables in try-with-resources.Looper.getMainLooper(). LiveData.setValue() called only on main thread; postValue() from worker threads. All handlers cleared in onDestroy() or equivalent lifecycle method.onDestroyView(). LiveData observers attached with getViewLifecycleOwner(). No observers on this Fragment reference.areItemsTheSame() based on stable ID, submitList() with new list instance, no mutation of submitted list.Closeable, Cursor, ParcelFileDescriptor, TypedArray opened in try-with-resources or explicit close in finally..printStackTrace(). InterruptedException restores interrupt flag and returns. Other exceptions logged or handled with state update.NewApi, threading annotation and resource-leak warnings after ./gradlew lint.Thread.sleep() or real executors. Direct executors or InstantTaskExecutorRule. Espresso uses IdlingResource for async work, not waits.Data format: Java source files (.java), test files (.java), and package-info.java with @NullMarked annotation. Configuration: build.gradle or build.gradle.kts with compileOptions, isCoreLibraryDesugaringEnabled, and dependency on desugar_jdk_libs where applicable.
You know the skill worked when:
NewApi, threading annotation and resource-leak errors; ./gradlew lint output shows no blockers../gradlew testDebugUnitTest and ./gradlew connectedAndroidTest are green.static fields holding Context, Activity, View, Fragment, adapters or listeners.new Handler(), AsyncTask, Executors.new... inside an Activity or Fragment.observe(this, ...) in a Fragment; ViewBinding fields never cleared.notifyDataSetChanged() for list updates; mutating a list already passed to submitList.catch (Exception e) { e.printStackTrace(); } and empty InterruptedException handlers.Log.d (escalate to mobile-security).minSdk and core library desugaring checked before using a JDK APINewApi clean; no blanket suppressions@NullMarked or public signatures annotated with @Nullable / @NonNullLooper.getMainLooper() and are cleared; no AsyncTask in new codeonCleared()setValue on main thread, postValue from workers; LiveData exposed read-onlygetViewLifecycleOwner(); binding nulled in onDestroyView()ListAdapter + DiffUtil with id-based areItemsTheSameequals and hashCode overridden togetherInstantTaskExecutorRule; Espresso uses IdlingResource, not sleepsmobile-kotlin - target idioms when migrating, and Kotlin-side interop rulesmobile-mvvm - ViewModel and LiveData or StateFlow architecturemobile-design-patterns - Builder, Factory and Strategy where Java needs themmobile-code-review - review workflow and severity levelsmobile-security - storage, logging and network rules for legacy Java codemobile-ci-cd - lint and test gates in the build pipelinemobile-testing - cross-platform test strategy| Feature | Java | On Android |
|---|---|---|
| Lambdas, method references, default and static interface methods | 8 | Desugared by D8 for every API level |
var, switch expressions, text blocks, instanceof patterns |
10-16 | Compile-time features, every API level |
| Records | 16 | Supported by current AGP (D8 desugars them). Check reflection-based JSON libraries |
| Sealed classes and interfaces | 17 | Only on recent AGP / D8. Verify before relying on them |
Pattern matching for switch, record patterns |
21 | Needs Java 21 source level and AGP / D8 support. Verify first |
java.time (API 26), java.util.stream, Optional, java.util.function (API 24) |
library | Below those API levels only with core library desugaring |
Newer JDK methods (List.of, List.getFirst(), Stream.toList()) |
library | Tied to newer API levels. Trust lint NewApi; desugaring covers only part |
The JDK that runs Gradle (17+ for current AGP) is separate from the language level and from the APIs available at runtime. The runtime APIs come from the device's Android version.
// BAD: compiles against a recent compileSdk, crashes on older devices (added to Android in API 35)
Device first = devices.getFirst();
// GOOD
Device first = devices.get(0);
// GOOD: java.time on minSdk below 26 through core library desugaring (app/build.gradle.kts)
android {
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
dependencies { coreLibraryDesugaring(libs.desugar.jdk.libs) }
Never suppress NewApi without a Build.VERSION.SDK_INT check or confirmed desugaring.
// BAD: public mutable fields, internal list shared with callers
public class Device {
public String name;
public List<String> tags;
}
// GOOD: record with a defensive copy (works on every API level)
public record Device(String id, String name, List<String> tags) {
public Device {
Objects.requireNonNull(id);
tags = Collections.unmodifiableList(new ArrayList<>(tags));
}
}
No records? final class, private final fields, no setters, copies in and unmodifiable views out. static final constants only for truly immutable values. A static final List is still mutable.
// package-info.java: everything in the package is non-null unless marked
@NullMarked
package com.example.devices;
import org.jspecify.annotations.NullMarked;