Java में Records
In this page:
record RecordName(Type component1, Type component2) { }
RecordName object = new RecordName(value1, value2);
object.component1();
Introduction to Records
एक Java record एक खास, compact तरह की class है जिसे specifically एक simple, immutable data carrier के रूप में काम करने के लिए डिज़ाइन किया गया है। एक record declare करने से अपने आप private final fields, एक canonical constructor, accessor methods, और सही hashCode(), equals(), और toString() implementations बन जाते हैं, वह सब बिना हाथ से वह boilerplate लिखे।
उदाहरण: Introduction to Records
// Define the class Main
public class Main {
// Define the record Point (an immutable data carrier)
record Point(int x, int y) {}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p
Point p = new Point(3, 4);
// Print a line to the console
System.out.println(p.x() + "," + p.y() + " " + p);
}
}
Login to try C/C++/Java/PHP code in the editor
Compact Constructors
आप किसी record में एक compact constructor जोड़ सकते हैं ताकि उसकी data store होने से पहले जांची या normalize की जा सके, बिना उस constructor के signature में record की parameter list फिर से declare किए। यह invalid input को reject करने की idiomatic जगह है, जैसे किसी negative age पर throw करना।
उदाहरण: Compact Constructors
// Define the class Main
public class Main {
// Define the record Person (an immutable data carrier)
record Person(String name, int age) {
Person {
if (age < 0) throw new IllegalArgumentException("age cannot be negative");
}
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Person object and store it in p
Person p = new Person("Anya", 25);
// Print a line to the console
System.out.println(p);
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Methods and Fields
आप किसी record के अंदर एक साधारण class की तरह ही custom instance methods, static methods, और static fields define कर सकते हैं। हालांकि, आप record के header में पहले से listed fields के अलावा कोई अतिरिक्त instance fields declare नहीं कर सकते, क्योंकि इससे इसकी एक साधारण, निश्चित data shape होने की गारंटी टूट जाएगी।
उदाहरण: Custom Methods and Fields
// Define the class Main
public class Main {
// Define the record Circle (an immutable data carrier)
record Circle(double radius) {
static final double PI = 3.14159;
double area() {
// Send PI * radius * radius back to the caller
return PI * radius * radius;
}
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Circle object and store it in c
Circle c = new Circle(2);
// Print a line to the console
System.out.println(c.area());
}
}
Login to try C/C++/Java/PHP code in the editor
Implementing Interfaces
Records implicitly final हैं, यानी वे कभी किसी दूसरी class को extend नहीं कर सकतीं, क्योंकि subclassing की अनुमति देने से उनकी immutability की गारंटी कमज़ोर हो जाएगी। हालांकि, वे किसी भी संख्या में interfaces implement कर सकती हैं, जो किसी record को अतिरिक्त behavior या contracts देने का तरीका है।
उदाहरण: Implementing Interfaces
// Define the class Main
public class Main {
// Define the interface Shape
interface Shape {
double area();
}
// Define the record Square (an immutable data carrier)
record Square(double side) implements Shape {
public double area() { return side * side; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Square object and store it in s
Shape s = new Square(4);
// Print a line to the console
System.out.println(s.area());
}
}
Login to try C/C++/Java/PHP code in the editor
Nested and Local Records
आप किसी दूसरी class के अंदर nested, या किसी एक method के scope के अंदर locally भी एक record declare कर सकते हैं एक quick, throwaway data shape के लिए। किसी method के अंदर declared local records implicitly static होते हैं, इसलिए वे enclosing instance को capture नहीं कर सकते।
उदाहरण: Nested and Local Records
public class Main {
record Outer(int value) {
record Inner(String label) {} // nested record
}
public static void main(String[] args) {
record Local(int n) {} // local record, implicitly static
Local l = new Local(7);
System.out.println(l);
}
}
Login to try C/C++/Java/PHP code in the editor
- कोई setter call करने या किसी record field को बदलने की कोशिश करना, जबकि सभी record components final होते हैं इसलिए एक record को बनने के बाद modify नहीं किया जा सकता।
p.getX()call करना, जबकि किसी record का accessor component के नाम पर होता है, इसलिए यहp.x()है।- किसी record को दूसरी class extend करवाने या अतिरिक्त instance fields declare करवाने की कोशिश करना, जिसकी अनुमति नहीं है क्योंकि records पहले से
Recordको extend करती हैं और सिर्फ अपने components रखती हैं।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: