Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungKI half beim Finden: iOS 27 schließt mehr als 100 Sicherheitslücken(21.09.2026 um 06:00 Uhr)
Sichere ProgrammierungWhat Is Rowhammer? How Can Repeated Memory Access Flip Bits in RAM?(21.09.2026 um 07:12 Uhr)
Sichere Programmierungnpm publish Ignores .gitignore: The .npmignore Override Rule(21.09.2026 um 07:15 Uhr)
Sichere ProgrammierungAphelion Editor - A free node-based video / VFX editor(21.09.2026 um 07:21 Uhr)
Sichere ProgrammierungGovernance Attack Surface Review: OKX(21.09.2026 um 07:31 Uhr)
Sichere ProgrammierungJSM Portal Request Create Property Panel Submit(21.09.2026 um 07:34 Uhr)
Reverse Engineeringsearch instructions assembly easy (X86,RISCV,AARCH64,etc)(20.09.2026 um 15:44 Uhr)
Sichere ProgrammierungKI half beim Finden: iOS 27 schließt mehr als 100 Sicherheitslücken(21.09.2026 um 06:00 Uhr)
Sichere ProgrammierungWhat Is Rowhammer? How Can Repeated Memory Access Flip Bits in RAM?(21.09.2026 um 07:12 Uhr)
Sichere Programmierungnpm publish Ignores .gitignore: The .npmignore Override Rule(21.09.2026 um 07:15 Uhr)
Sichere ProgrammierungAphelion Editor - A free node-based video / VFX editor(21.09.2026 um 07:21 Uhr)
Sichere ProgrammierungGovernance Attack Surface Review: OKX(21.09.2026 um 07:31 Uhr)
Sichere ProgrammierungJSM Portal Request Create Property Panel Submit(21.09.2026 um 07:34 Uhr)
Reverse Engineeringsearch instructions assembly easy (X86,RISCV,AARCH64,etc)(20.09.2026 um 15:44 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Java record compact constructors (steal back from ai)

Reagiere als Erste:r — dein Feedback zählt!

First in the series #stealbackfromai

you need validation for record or default values ?

I was silly trying this to define default values in a record:

public static record SomeCounts(UUID id, Long v1, Long v2, Long v3){
    SomeCounts(UUID id, Long v1, Long v2, Long v2){
        this(id,
                v1== null ? 0l : v1,
                v2== null ? 0l : v2,
                v3== null ? 0l : v3
                );
    }
}

It does not compiple (Canonical constructor cannot delegate to another constructor). Turns out there is nicer and cleaner way already provided called comapct constructor.

Compact constructors

Compact constructors in Java records provide a concise way to customize the canonical constructor (the default one matching the record's components) without repeating parameter names.

Syntax Basics
Declare it immediately after the record header, using just the record name in braces—no parameter list needed:

public static record SomeCounts(UUID id, Long v1, Long v2, Long v3){
    public SomeCounts{
        v1 = v1== null ? 0l : v1,
        v2 = v2== null ? 0l : v2,
        v3 = v3== null ? 0l : v3
        // Validation example: 
        //if (id == null) throw new IllegalArgumentException();
    }
}

The compiler implicitly supplies parameters matching the fields and handles field assignments after your body executes.12

Execution Flow

  1. Your compact body runs first (for validation/normalization).
  2. Compiler auto-assigns parameters to final fields.
  3. Superclass Record constructor completes.

You can modify parameters (like null checks), throw exceptions, or print logs, but cannot assign fields directly (this.v1 = ... fails compilation).3

Key Benefits

  • Zero boilerplate: No need to write this.field = field; everywhere.
  • Immutable safety: Fields stay final; changes only affect incoming parameters before final assignment.
  • Canonical only: Applies to the main constructor; doesn't affect static factories or other overloads.

Common Patterns

Use Case Example Code
Null-to-default v1 = (v1 != null) ? v1 : 0L; 4
Range validation if (v3 < 0) throw new IllegalArgumentException(); 5
Normalization id = id.toString().toLowerCase(); 5

This keeps your record cleaner while ensuring numeric fields are never null post-construction.1678910

  1. https://blogs.oracle.com/javamagazine/post/java-record-compact-canonical-constructor ↩

  2. https://docs.oracle.com/en/java/javase/16/language/records.html ↩

  3. https://stackoverflow.com/questions/74083123/java-record-compact-constructor-bytecode ↩

  4. https://xebia.com/blog/how-to-use-java-records/ ↩

  5. https://mikemybytes.com/2022/02/16/java-records-and-compact-constructors/ ↩

  6. https://www.reddit.com/r/java/comments/stte7o/java_records_compact_constructors/ ↩

  7. https://stackoverflow.com/questions/77582274/throw-exception-in-records-compact-constructor ↩

  8. https://www.baeldung.com/java-records-custom-constructor ↩

  9. https://coderanch.com/t/754107/certification/understand-Records-comapact-constructor-overloaded ↩

  10. https://www.youtube.com/watch?v=i0p1B7L3FW8 ↩

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Java record compact constructors (steal back from ai)

Thematisch verwandte Begriffe: Java, record, compact, constructors · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94109 | openEQUELLA versions before 2026.1.0 contain a remote code execution vul…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick