1

I am looking to avoid some boilerplate code. I see that jdk14 has Records as shown in the example below.

https://www.logicbig.com/tutorials/core-java-tutorial/java-14-changes/records.html

How can I do something similar to Records in jdk11 ?

Thar
  • 2,956
  • 2
  • 10
  • 9
  • Are you asking for a library to use here? – Naman Apr 23 '20 at 05:39
  • Actually, [Records](https://openjdk.java.net/jeps/359) is a preview feature in Java 14. You must enable that feature, off by default. The specification is subject to change or withdrawal. – Basil Bourque Apr 23 '20 at 05:47
  • @Naman A lib or some custom code is what I was looking for. Thank you! The solutions below look promising. – Thar Apr 23 '20 at 13:59

2 Answers2

5

I highly recommend Project Lombok (site here https://projectlombok.org/). From the example you listed

 public record Person(String name, String gender, int age) {}

That can be done via Lombok like this

import lombok.Data

@Data
public class Person {
    private String name;
    private String gender;
    private int age;
}

Lombox creates getters and setters, toString, hashCode, and a default constructor, as well as one with all the orgs.

UPDATE: It was pointed out in the comments that records are immutable. You can easily achieve this with lombok in a couple of ways. Option 1:

import lombok.Data

@Data
public class Person {
    private final String name;
    private final String gender;
    private final int age;
}

That will add, again, the required-args constructor. @Data does not, for relatively obvious reasons, create setters for final fields.

Option 2 is a little more explicit:

import lombok.*;

@RequiredArgsConstructor
@EqualsAndHashCode
@Getter
public class Person {
    private String name;
    private String gender;
    private int age;
}
Josh Ghiloni
  • 1,260
  • 8
  • 19
  • Java 14 records are immutable, so are autovalue objects.Lombox doesn't seem to offer that option. – LeffeBrune Apr 23 '20 at 04:58
  • 3
    I believe [`@Value` gets you closest to Records](https://stackoverflow.com/questions/61306802/lombok-getter-setter-vs-java-14-record). – Naman Apr 23 '20 at 05:41
  • You actually could, @LeffeBrune. You could replace the `@Data` annotation and replace it with `@Constructor @Getters`, which would make them immutable. – Josh Ghiloni Apr 24 '20 at 01:26
  • what is `lombox` in imports ? and why is there `()` in class name `Person()` ? It doesn't work for me. – SRJ Feb 22 '23 at 21:05
  • @shubham `lombok` is a tool that generates methods at compile time. In the case of this example, it adds a constructor with `name`, `gender`, and `age` arguments, as well as `getX` methods for each, effectively allowing a user to set the values at construction time only. The `()` after `Person` are a typo. I'll fix, thanks for pointing it out – Josh Ghiloni Mar 06 '23 at 02:07
  • @Josh thanks i am familiar with Lombok and have used it extensively but I saw imports as `lombox` which got me confused. But now answer seems updated. – SRJ Mar 06 '23 at 09:00
  • @shubham yep, just typos! – Josh Ghiloni Mar 07 '23 at 12:47
0

You can use code generation. It's not as concise as Java 14, but better than nothing. AutoValue is pretty amazing.

import com.google.auto.value.AutoValue;

@AutoValue
abstract class Animal {
  static Animal create(String name, int numberOfLegs) {
    return new AutoValue_Animal(name, numberOfLegs);
  }

  abstract String name();
  abstract int numberOfLegs();
}
LeffeBrune
  • 3,441
  • 1
  • 23
  • 36