Record Patten with Switch in Java 19
Last Updated :
24 Apr, 2025
In this article, we will learn how to use the Record Pattern with Switch statements in Java 19 to create more maintainable, readable code. With the Record Pattern and Switch, we can create cleaner and more efficient code, making our programming experience more productive. As we all know switch statements help to allow for pattern matching. So we can use it for pattern matching to match against specific properties of objects.
What is Record in Java?
In Java, Record is a type of class that is designed to handle immutable data. A record is Declared using the record keyword. Here you can learn more about Record in Java.
Syntax:
public record Person(int Name, int age) {}
Here you can learn more about Java Switch statement. For example, we are going to see how can we match an Employee Record by the Name of the Employee.
Step By Step Implementation
Step 1: Creating a Java Project
To Create a Java Project in IntelliJ Idea you can refer to Creating First Java Application in IntelliJ IDEA in this article. If you wish to use other IDE then you can also use.
Step 2: Create a Record Class
In this step, we are going to Create an Employee Record Class using the record keyword. It Contains four fields such as employee name, Age, Salary, and Designation. Below is the code for Employee Record in Java Comments are added for a better understanding of the Code.
Java
// EmployeeRecord class with four fields
public record EmployeeRecord(String name, int age,int Salary,String designation) {}
Step 3: Creating Switch Cases
In this step, we are going to create Switch Cases to Display an Employee Record by Employee Name. Below is the function for Switch case implementation. Comments are added for Better Understanding.
Java
// funtion for print Employee Details by Employee Name
public static void printEmployeeDetails(EmployeeRecord employee) {
// Switch Case to match Employee Name and print Details
switch(employee.name()) {
case "Ramcharan": {
System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
break;
}
case "Chinmaya Mohapatra": {
System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
break;
}
case "Sam": {
System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
break;
}
case "Sai": {
System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
break;
}
default: {
System.out.println("Employee Not Found.");
break;
}
}
}
Step 4: Calling printEmployeeDetails Function
Here we have to call the printEmployeeDetails Method prepared in the Previous step and we have to make some EmployeeRecord Object and pass these objects to the printEmployeeDetails function for Record Matching. Below is the Code. Comments are added for a Better Understanding.
Java
public class Main {
public static void main(String args[]){
// Creating some objects of EmployeeRecord
EmployeeRecord ram = new EmployeeRecord("Ramcharan", 25,30000,"SDE I");
EmployeeRecord chinmaya = new EmployeeRecord("Chinmaya Mohapatra", 30,50000,"SDE II");
EmployeeRecord sam = new EmployeeRecord("Sam", 40,80000,"SDE II");
EmployeeRecord sai = new EmployeeRecord("Sai", 24,60000,"SDE I");
// Calling printEmployeeDetails funtion
printEmployeeDetails(ram);
printEmployeeDetails(chinmaya);
printEmployeeDetails(sam);
printEmployeeDetails(sai);
}
// funtion for print Employee Details by Employee Name
public static void printEmployeeDetails(EmployeeRecord employee) {
// Switch Case to match Employee Name and print Details
switch(employee.name()) {
case "Ramcharan": {
System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
break;
}
case "Chinmaya Mohapatra": {
System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
break;
}
case "Sam": {
System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
break;
}
case "Sai": {
System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
break;
}
default: {
System.out.println("Employee Not Found.");
break;
}
}
}
}
Output:
Ramcharan is 25 years old, Salary = 30000 Designation = SDE I
Chinmaya Mohapatra is 30 years old, Salary = 50000 Designation = SDE II
Sam is 40 years old, Salary = 80000 Designation = SDE II
Sai is 24 years old, Salary = 60000 Designation = SDE I
Process finished with exit code 0
Similar Reads
Record Pattern with instanceof in Java 19
In Java, the instanceof operator is used to check whether an object is an instance of a particular class or interface. It returns a boolean value of true if the object is an instance of the specified class or interface, and false otherwise. Example of using instanceof Java public class Example { pub
3 min read
Pattern Matching for Switch in Java
Pattern matching for a switch in Java is a powerful feature that was introduced in Java 14. Before this update, a switch expression could only be used to match the type of a value, rather than its actual value. However, by comparing the model, developers can now match the values ââof Strings, enums,
8 min read
Switch Statements in Java
The switch statement in Java is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.It is an alternative to an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of the code based on the v
9 min read
Shift Operator in Java
Operators in Java are used to performing operations on variables and values. Examples of operators: +, -, *, /, >>, <<. Types of operators: Arithmetic Operator,Shift Operator,Relational Operator,Bitwise Operator,Logical Operator,Ternary Operator andAssignment Operator. In this article, w
4 min read
Stack set() method in Java with Example
The set() method of Java Stack is used to replace any particular element in the stack created using the Stack class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: public E set(int index
3 min read
LogRecord setThrown() method in Java with Examples
The setThrown() method of java.util.logging.LogRecord is used to a throwable associated with the log event.This is used to log Exceptions in the logRecord that can be used for logging messages. Syntax: public void setThrown(Throwable thrown) Parameters: This method accepts thrown as a parameter whic
2 min read
What is a Switch Port?
A network switch is a piece of hardware that allows computers to communicate with one another. It accepts physical connectors from computers and other network devices and then uses packet switching to receive and forward data. Connecting different devices to the ports on a network switch allows them
2 min read
Java Swing | JSeparator with examples
JSeparator is a part of Java Swing framework. It is used to create a dividing line between two components. More specifically, it is mainly used to create dividing lines between menu items in a JMenu. In JMenu or JPopupMenu addSeparartor function can also be used to create a separator. Constructor of
4 min read
Field setChar() method in Java with Examples
setChar() method of java.lang.reflect.Field used to set the value of a field as a char on the specified object. When you need to set the value of a field of an object as char then you can use this method to set value over an Object. Syntax: public void setChar(Object obj, char c) throws IllegalArgum
3 min read
LogRecord setInstant() method in Java with Examples
The setInstant() method of java.lang.reflect.LogRecord is used to set this instant that the event occurred this is helpful to record logging events instant. An arithmeticException will be thrown if the given instant represents a point on the timeline too far in the future or past to fit in long mill
2 min read