In data-oriented applications, managing object states efficiently is crucial. This project demonstrates the implementation of a Java Record to act as a simple data carrier for book information. By using the record keyword, we leverage built-in immutability and automatic accessor methods. This approach simplifies the code and ensures that each book entry remains consistent throughout the application's lifecycle.
- Data Carrier: Defined a
Bookrecord to encapsulate title and author. - Immutability: Utilized the record's final nature to protect data integrity.
- Accessor Usage: Used generated methods
title()andauthor()for data retrieval.
- Java 16+ (Records)
- Book: A record representing a single library entry.
- LibraryApp: The main entry point for creating and verifying book data.
Title: Effective Java
Author: Joshua Bloch
Project Structure:
JavaBasics_Task_389/
├── src/
│ └── com/yurii/pavlenko/
│ ├── app/
│ │ └── LibraryApp.java
│ └── library/
│ └── models/
│ └── Book.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md
Code
package com.yurii.pavlenko.app;
import com.yurii.pavlenko.library.models.Book;
public class LibraryApp {
public static void main(String[] args) {
Book favoriteBook = new Book("Effective Java", "Joshua Bloch");
System.out.println("Title: " + favoriteBook.title());
System.out.println("Author: " + favoriteBook.author());
}
}package com.yurii.pavlenko.library.models;
public record Book(String title, String author) {
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT