-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordExample.java
More file actions
36 lines (26 loc) · 1.13 KB
/
RecordExample.java
File metadata and controls
36 lines (26 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package DataStructures;
/**
* Records in java are introduced in 14
* Record extends from class and record must be in it's own file
* @author Srinvas Vadige, srinivas.vadige@gmail.com
* @since 21 Sept 2024
*/
public record RecordExample(int foo, String bar) {
public RecordExample setFoo(int newFoo){ // optional but it has to be public for setting as it record IMMUTABLE
return new RecordExample(newFoo, bar);
}
RecordExample setBar(String newBar){ // optional
return new RecordExample(foo, newBar);
}
public RecordExample{} // optional
}
/*
In many cases, this data is immutable, since immutability ensures the validity of the data without synchronization.
To accomplish this, we create data classes with the following:
private, final field for each piece of data
getter for each field
public constructor with a corresponding argument for each field
equals method that returns true for objects of the same class when all fields match
hashCode method that returns the same value when all fields match
toString method that includes the name of the class and the name of each field and its corresponding value
*/