Thumbnail image

Raise your Java code using Records

8/1/2024 6-minute read

One of the most awaited feature by the Java community, now is available for everyone.

Introduced on Java 14, Records is a new type launched for Java.

It was improved in Java 15 and finished in Java 16, now with the Java 17 he finally is official, let’s know a little more about this new resource that promise help us in our projects with Java.

As we know, in Java we have classes, interfaces, and enums, but now we also have Records. First, for coding the example of this article you need to have installed the minimum Java 14 in your PC. The example was made using the Intellij Idea from JetBrains and the Java version 17. All the code can be found in this repository in my GitHub, you can clone the project or just build from zero following the steps of the article.

Let’s go, after clone the project, in Intellij, we have a new project with the name java_record. In the folder /src let’s click with the right button of the mouse or we can use the shortcut ctrl + n to create a new java class. As we can see, we have to choose between Class, Interface, Enum, Annotation and the new option Record. Let’s select this option, and we will give to this Record the name Costumer.

image

dialog box

After that the IDE will generate a simple java class(record).

public record Costumer() {
}

As we can see, we have a parentheses “( )” just after the name of record. This parentheses works like a Constructor. I mean, inside this parentheses we can declare all properties we want for this class, in this case, for this record. So, let’s add some parameters for our record. It will be like this.

    public record Costumer(Long id, String name, String phoneNumber) {
    }

Ok, now, just to think about the benefits that record gave for us, let’s create a new java class, but this time will not be a record. We will make the same process we did before, at the directory src/ let’s click with the right button of the mouse or use the shortcut ctrl + n to create a new java class. Let’s give the name Costumer2.

Now, let’s create the properties of the class.

	public class Costumer2 {
		
		private Long id;
		private String name;
		private String phoneNumber;
	}

After that, let’s create the constructor with all the arguments of this class. In intellij we have the shortcut alt + insert. Select the constructor, check all arguments.

image

all properties selected

Will be like this,

	public class Costumer2 {
		
		private Long id;
		private String name;
		private String phoneNumber;
	}

	public Costumer2(Long id, String name, String phoneNumber) {
		this.id = id;
		this.nome = name;
		this.phoneNumber = phoneNumber;
	}

Continuing our class, now we will generate the getters. Let’s repeat the same process alt + insert. Select Getter, choose all the attributes. Following the same process, we can generate the equals and HashCodes. Also, we can generate the method To String.

So, as we can see, we created a class that we declared only 3 properties, all then are private. Inserting the Constructors, the methods Getters, equals and HashCode and plus the To String, we have a class with 50 lines of code, and we didn’t write any logic, just a basic of a class.

We can see, records are the huge shortcut to create a java class. When the Java compile this record, we will have a class Costumer that extends a new type of Java called record. The records cannot extends any other classes, but we can implement any necessary Interfaces. Other important thing, records are immutables. That means all the attributes declared inside a record, are Final. So, just for example, let’s reproduce this in our class Costumer2. Will be like this:

	public class Costumer2 {
		
		private final Long id;
		private final String name;
		private final String phoneNumber;
	}

	public Costumer2(Long id, String name, String phoneNumber) {
		this.id = id;
		this.nome = name;
		this.phoneNumber = phoneNumber;
	}

Because of that, we have only the constructor with all arguments and don’t have an empty constructor in a Record. We can conclude, a Record also doesn’t have a methods Setters, because the attributes cannot be changed, we cannot do a ‘set’ in any attribute inside a Record. Another thing we need to be aware is the syntax of methods getters. By default, the getters methods inside a record do not have a convention get before the attribute. Like I show below:

	public Long Id() {
        return id;
    }

    public String name() {
        return name;
    }

    public String phoneNumber() {
        return phoneNumber;
    }

Now we can see one of the benefits of a Record, our Record have only one line of code against the same java class with almost fifty lines of code. But probably you should question yourself, without the get convention how can I use this class?

Ok, let’s create a Main class, first we will create a method and inside this method we will instantiate our record. Now, will call the method inside our method main. The class ill be like this:

image

Main class java

Now, let’s run the application, in intellij press ctrl + shift + F10 and let’s see the terminal output.

image

terminal output

We can see in the output terminal, we even don’t need to create a method to string, the output already came formatted with the to string implementation. So, let’s recap. A Record is a java class that extends a class Record that’s why we can’t extend others classes. But we can implement interfaces and also declare methods inside her. Below you can see a Record with an interface implemented, and a method declared inside her.

    import java.io.Serializable;
    
    public record Costumer(Long id, String name, String phoneNumber) implements Serializable {
    
        public static int COUNT = 0;
    
        public void myMethod() {}
    }

So, we saw the advantages about this new type of java called Records, and he can help us a lot in our java applications. But we need be aware with some limitations. Because all the attributes are final, it doesn’t have the empty Constructor and also we don’t have the methods Setters, we can’t use the Records with Hibernate and JPA. Probably now you are questioning yourself, how can I use the Record with Spring and Springboot? So, stay tuned because this question will be answered soon here in the RUYDEVBLOG. I hope you like and learned something, you can share the article with others friends developers, you can also give some comment below. See you in the next article.