# 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;
	}
}
//...

🛠️ Auto-refactor Available

You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

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