← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 3 of 12

Java में Records

एक record एक साधारण class बनाने का छोटा तरीका है जो सिर्फ data रखती है, जैसे एक तैयार-बना form। Java routine हिस्से आपके लिए लिख देता है।
Syntax
java
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

java
// 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);
	}
}

Compact Constructors

आप किसी record में एक compact constructor जोड़ सकते हैं ताकि उसकी data store होने से पहले जांची या normalize की जा सके, बिना उस constructor के signature में record की parameter list फिर से declare किए। यह invalid input को reject करने की idiomatic जगह है, जैसे किसी negative age पर throw करना।

उदाहरण: Compact Constructors

java
// 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);
	}
}

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

java
// 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());
	}
}

Implementing Interfaces

Records implicitly final हैं, यानी वे कभी किसी दूसरी class को extend नहीं कर सकतीं, क्योंकि subclassing की अनुमति देने से उनकी immutability की गारंटी कमज़ोर हो जाएगी। हालांकि, वे किसी भी संख्या में interfaces implement कर सकती हैं, जो किसी record को अतिरिक्त behavior या contracts देने का तरीका है।

उदाहरण: Implementing Interfaces

java
// 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());
	}
}

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

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. कोई setter call करने या किसी record field को बदलने की कोशिश करना, जबकि सभी record components final होते हैं इसलिए एक record को बनने के बाद modify नहीं किया जा सकता।
  2. p.getX() call करना, जबकि किसी record का accessor component के नाम पर होता है, इसलिए यह p.x() है।
  3. किसी record को दूसरी class extend करवाने या अतिरिक्त instance fields declare करवाने की कोशिश करना, जिसकी अनुमति नहीं है क्योंकि records पहले से Record को extend करती हैं और सिर्फ अपने components रखती हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.