java 128 lines · 2 tabs

Record types for immutable data

David Kumar Jan 2026
2 tabs
package com.example.demo.dto;

import java.time.LocalDateTime;
import java.util.List;

// Simple record
public record UserDTO(
    Long id,
    String name,
    String email
) {}

// Record with validation
public record CreateUserRequest(
    String name,
    String email,
    String password
) {
    // Compact constructor for validation
    public CreateUserRequest {
        if (name == null || name.isBlank()) {
            throw new IllegalArgumentException("Name cannot be blank");
        }
        if (email == null || !email.contains("@")) {
            throw new IllegalArgumentException("Invalid email format");
        }
        if (password == null || password.length() < 8) {
            throw new IllegalArgumentException("Password must be at least 8 characters");
        }
    }
}

// Record with computed fields
public record Money(double amount, String currency) {

    public Money add(Money other) {
        if (!currency.equals(other.currency)) {
            throw new IllegalArgumentException("Currency mismatch");
        }
        return new Money(amount + other.amount, currency);
    }

    public String formatted() {
        return String.format("%s %.2f", currency, amount);
    }
}

// Record implementing interface
public interface Identifiable {
    Long getId();
}

public record Post(
    Long id,
    Long userId,
    String title,
    String content,
    LocalDateTime createdAt
) implements Identifiable {

    @Override
    public Long getId() {
        return id;
    }

    public boolean isRecent() {
        return createdAt.isAfter(LocalDateTime.now().minusDays(7));
    }
}

// Nested records
public record Order(
    Long id,
    Customer customer,
    List<LineItem> items,
    Money total
) {
    public record Customer(String name, String email) {}
    public record LineItem(String product, int quantity, Money price) {}
}

// Record with static factory method
public record ApiResponse<T>(
    boolean success,
    T data,
    String message,
    int statusCode
) {
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(true, data, "Success", 200);
    }

    public static <T> ApiResponse<T> error(String message, int statusCode) {
        return new ApiResponse<>(false, null, message, statusCode);
    }
}
2 files · java Explain with highlit

Java Records (JDK 14+) provide concise syntax for immutable data carriers. Records automatically generate constructor, getters, equals, hashCode, and toString. I use records for DTOs, value objects, and API responses. Records are final and all fields are final by default. Compact constructor syntax validates or normalizes data. Records can implement interfaces and have static methods. They work perfectly with pattern matching and sealed types. Serialization libraries like Jackson support records natively. Records reduce boilerplate compared to traditional classes, improving code clarity. They're ideal for representing pure data without behavior. Spring Boot integrates records smoothly for request/response handling. Records modernize Java code, making it more expressive and maintainable.


Related snips

graphql
type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]!
    createdAt: String!

GraphQL API with Spring Boot

java graphql spring-boot
by David Kumar 3 tabs
java
package com.example.starter.config;

import com.example.starter.properties.CustomProperties;
import com.example.starter.service.CustomService;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

Custom Spring Boot starters

java spring-boot starter
by David Kumar 4 tabs
java
package com.example.demo.config;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

Messaging with Apache Kafka

java kafka messaging
by David Kumar 3 tabs
java
package com.example.demo.controller;

import com.example.demo.dto.FileMetadata;
import com.example.demo.service.FileStorageService;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;

File upload and download handling

java spring-boot file-upload
by David Kumar 2 tabs
sql
CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    username VARCHAR(100) NOT NULL UNIQUE,
    email VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

Database migration with Flyway

java flyway database-migration
by David Kumar 6 tabs
java
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

WebSocket for real-time communication

java websocket spring-boot
by David Kumar 3 tabs

Share this code

Here's the card — post it anywhere.

Record types for immutable data — share card
Link copied