# Use Java Records

# Description

Since Java 16, record classes are a new kind of class in the Java language. Record classes help to model plain data aggregates with less ceremony than normal classes. This rule replaces the declarations of:

  • local classes
  • inner classes
  • package private root classes

with record class declarations. Thus dropping some boilerplate code, guaranteeing immutability in language level, and providing better compatibility with the new or upcoming features, e.g., Pattern Matching.

Requirements

  • Java 16

# Benefits

Guarantees immutability. Eliminates boilerplate code. Encourages better serialization. Boosts compatibility with upcoming features.

# Tags

# Code Changes

# Local Classes

Pre

void plainGeometry() {
	// ...
	class Point {
		private final int x;
		private final int y;

		Point(int x, int y) {
		this.x = x;
			this.y = y;
		}

		public int x() {
			return x;
		}

		public int y() {
			return y;
		}
	}
	// ...
}

Post

void plainGeometry() {
	// ...
	record Point(int x, int y) {}
	//...
}

# Nested Classes

Pre

public class PlainGeometry {
	// ...
	private static final class Point {
		private final int x;
		private final int y;

		Point(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public int x() {
			return x;
		}

		public int y() {
			return y;
		}
	}
	// ...
}

Post

public class PlainGeometry {
	//...
	private record Point(int x, int y) {}
	//...
}

# Multiple Constructors

Pre

// ...
private static final class Point {
	private final int x;
	private final int y;

	Point(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	Point() {
		this(0,0);
	}

	public int x() {
		return x;
	}

	public int y() {
		return y;
	}
}
// ...

Post

//...
private record Point(int x, int y) {
	
	Point() {
		this(0,0);
	}
}
//...

# Parameterized Inner Classes

Pre

//...
private static final class GenericWrapper<T> {
	private final T value;

	public GenericWrapper(T value) {
		this.value = value;
	}

	public T value() {
		return value;
	}
}
//...

Post

//...
private record GenericWrapper<T> (T value) {}
//...

# Additional Methods

Pre

// ...
private static final class Person {
	private final String firstName;
	private final String lastName;

	Person(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public String firstName() {
		return firstName;
	}

	public String lastName() {
		return lastName;
	}
	
	public String name() {
		return firstName + " " + lastName;
	}			
}
// ...

Post

//...
private record Person(String firstName, String lastName) {
	
	public String name() {
		return firstName + " " + lastName;
	}
}
//...

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

Property Value
Rule ID UseJavaRecords
First seen in jSparrow version 4.5.0
Minimum Java version 16
Remediation cost 20 min
Links