Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an expected date for when Java data classes will be introduced?

Tags:

java

Scala (case classes) and C# (structs) have support for data classes. When is Java expected to offer support for this language feature?

like image 258
Rasheed Avatar asked Oct 04 '19 03:10

Rasheed


People also ask

Are there data classes in Java?

Data classes, i.e. Java classes whose sole purpose is to hold data and make it accessible via getters and setters, are among the largest collection points of boilerplate code in many software projects.

How do you create a data class in Java?

You have to create a Java class with private fields. And getter and setter methods for the fields. And additional methods like equals() , hashCode() and toString() . But who says you have to create the Java code by hand?

What does new Date () return in Java?

out. print(new Date()); I know that whatever is in the argument is converted to a string, the end value that is, new Date() returns a reference to a Date object.

When did Java add records?

Record was introduced in Java SE 14 as a preview feature, which is a feature whose design, implementation, and specification are complete but it is not a permanent addition to the language, which means that the feature may or may not exist in the future versions of the language.


2 Answers

With a bit of reseach, I ended up with Data Classes and Sealed Types for Java by Brian Goetz.

Here is the JEP, Records (Preview), linking to the above.

Summary - It is just an idea/JEP. So, we cannot tell when it would be implemented (or if it would be implemented at all)

like image 153
user7 Avatar answered Sep 27 '22 20:09

user7


The tentative date is March 2020 for the release of JDK 14.

In Nov. 2019, the JEP 359 about Records (data carriers classes) has been confirmed as being part of Java 14 as a preview (meaning, as commented below by Basil Bourque, it needs to be enabled explicitly and it can be removed in the future).

Still: you can start experimenting with it starting with Java 14, March 2020.

As presented here, record Range(int lo, int hi) would replace

package javax0.geci.tests.record;

import javax0.geci.annotations.Geci;

@Geci("record")
public final class Range {

    final  int  lo;
    final  int  hi;

    //<editor-fold id="record">

    public Range(final int lo, final int hi) {
        this.lo = lo;
        this.hi = hi;
    }

    public int getLo() {
        return lo;
    }

    public int getHi() {
        return hi;
    }

    @Override
    public int hashCode() {
        return java.util.Objects.hash(lo, hi);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Range that = (Range) o;
        return java.util.Objects.equals(that.lo, lo) && java.util.Objects.equals(that.hi, hi);
    }

    //</editor-fold>
}
like image 37
VonC Avatar answered Sep 27 '22 21:09

VonC