It seems that data class-like functions will be added in Java14

Many people have written classes in Java that have only fields, constructors, and getters (Setter, equals, toString in some cases). Such classes are called data classes. In some cases, you have to write a lot of similar classes, and no matter how much you use the method generation function of the IDE, it's annoying. If you use lombok, it will be alleviated to some extent, but in many cases you will have to set the IDE, etc., and it will still be troublesome.

Meanwhile, it seems that a function to solve this problem will be added in Java 14.

What's new in Java 14

Java 14 introduces something called Record. https://blogs.oracle.com/javamagazine/records-come-to-java

The usage seems to declare the class you want to be a data class as record instead of class.

Example of use

before use

FXOrderClassc.java


public final class FXOrderClassic {
    private final int units;
    private final CurrencyPair pair;
    private final Side side;
    private final double price;
    private final LocalDateTime sentAt;
    private final int ttl;

    public FXOrderClassic(int units, 
               CurrencyPair pair, 
               Side side, 
               double price, 
               LocalDateTime sentAt, 
               int ttl) {
        this.units = units;
        this.pair = pair; // CurrencyPair is a simple enum
        this.side = side; // Side is a simple enum
        this.price = price;
        this.sentAt = sentAt;
        this.ttl = ttl;
    }

    public int units() {
        return units;
    }

    public CurrencyPair pair() {
        return pair;
    }

    public Side side() {
        return side;
    }

    public double price() { return price; }

    public LocalDateTime sentAt() {
        return sentAt;
    }

    public int ttl() {
        return ttl;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) 
            return false;

        FXOrderClassic that = (FXOrderClassic) o;

        if (units != that.units) return false;
        if (Double.compare(that.price, price) != 0) 
            return false;
        if (ttl != that.ttl) return false;
        if (pair != that.pair) return false;
        if (side != that.side) return false;
        return sentAt != null ? 
            sentAt.equals(that.sentAt) : that.sentAt == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = units;
        result = 31 * result + 
                   (pair != null ? pair.hashCode() : 0);
        result = 31 * result + 
                   (side != null ? side.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + 
                   (int) (temp ^ (temp >>> 32));
        result = 31 * result + 
                   (sentAt != null ? sentAt.hashCode() : 0);
        result = 31 * result + ttl;
        return result;
    }

    @Override
    public String toString() {
        return "FXOrderClassic{" +
                "units=" + units +
                ", pair=" + pair +
                ", side=" + side +
                ", price=" + price +
                ", sentAt=" + sentAt +
                ", ttl=" + ttl +
                '}';
    }
}

I'm tired of seeing a lot of getters, constructors, and override methods side by side. This is what Java 14 does.

FXOrder.java


public record FXOrder(int units,
                      CurrencyPair pair,
                      Side side,
                      double price,
                      LocalDateTime sentAt,
                      int ttl) {}

~~ Like Kotlin ~~ You will find that you will be able to declare it very neatly. However, it seems that Setter is not prepared, so be careful when using OR mapper etc.

Summary

~~ I think Kotlin is good ~~ It seems that Java 14 will introduce a powerful new feature called Record. With this, it seems that legacy code can be rewritten concisely. Personally, I definitely want to use this feature, and I'm thinking of updating from Java 8!

Recommended Posts

It seems that data class-like functions will be added in Java14
[Java] It seems that `0 <hoge <10` cannot be written in the conditional expression of the ʻif` statement.
CORBA seems to be removed in Java SE 11. .. ..
Write a class that can be ordered in Java
Azure functions in java
Java (super beginner edition) that can be understood in 180 seconds
Importing Excel data in Java 2
Create Azure Functions in Java
Import Excel data in Java
Importing Excel data in Java 3
Reference memo / In-memory LDAP server that can be embedded in Java
Note that system properties including JAXBContext cannot be used in Java11
An active hash that can be treated as data even if it is not in the database
Java version notation that changes in Java 10
Display Firestore data in RecyclerView [Java]
[Java] December 28, 2020 will be formatted as December 28, 2021?
List of members added in Java 9
List of types added in Java 9
Library summary that seems to be often used in recent Android development (2019/11)
The story that the Servlet could not be loaded in the Java Web application
[Android Studio] Description that can be continuously input in SQLite Database [Java]
Continuation ・ Active hash that can be handled as data even if it is not in the database ~ Display