← Back to Core Java Course | Chapter 7: OOP Core | Lesson 7 of 11

Java के Getters और Setters

Getters और setters एक object के private data के safe doors हैं: एक देखने के लिए, एक बदलने के लिए। Setter पहले जांच सकता है कि नई value मायने रखती है या नहीं।
Syntax
java
private dataType field;

public dataType getField() {
    return field;
}

public void setField(dataType field) {
    this.field = field;
}

What are Getters and Setters?

Getters और setters public methods हैं जो किसी class की private fields तक controlled read/write access देते हैं, class को अपना internal data hidden रखने देते हुए जबकि बाहरी code को इसके साथ सुरक्षित रूप से interact करने देते हुए।

उदाहरण: What are Getters and Setters?

java
// Define the class Person
class Person {
	private String name;
	public String getName() {
		// Send name back to the caller
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
// Define the class Main
public class Main {
	// 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();
		p.setName("Alice");
		// Print a line to the console
		System.out.println(p.getName());
	}
}

Adding Validation in Setters

चूंकि एक setter regular code है, एक direct field assignment नहीं, यह incoming values को store करने से पहले validate कर सकता है — उदाहरण के लिए एक negative age या एक empty name reject करते हुए — जो एक bare public field कभी नहीं कर सकती थी।

उदाहरण: Adding Validation in Setters

java
// Define the class Person
class Person {
	private int age;
	public void setAge(int age) {
		// Check whether age < 0
		if (age < 0) {
			// Print a line to the console
			System.out.println("Invalid age");
			return;
		}
		this.age = age;
	}
	public int getAge() {
		// Send age back to the caller
		return age;
	}
}
// Define the class Main
public class Main {
	// 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();
		p.setAge(-5);
		p.setAge(25);
		// Print a line to the console
		System.out.println(p.getAge());
	}
}

Read-Only Classes

Setters छोड़ना और सिर्फ getters प्रदान करना एक class को construction के बाद effectively read-only बनाता है: callers इसकी state inspect कर सकते हैं लेकिन कभी नहीं बदल सकते, जो simple immutable-style value objects बनाने का एक उपयोगी तरीका है।

उदाहरण: Read-Only Classes

java
class Point {
	private final int x;
	Point(int x) {
		this.x = x;
	}
	public int getX() {
		return x; // no setter: read-only after construction
	}
}
public class Main {
	public static void main(String[] args) {
		Point p = new Point(5);
		System.out.println(p.getX());
	}
}

Write-Only Classes

Reverse pattern — getters के बिना setters — एक write-only class बनाता है, एक password जैसी sensitive values रखने के लिए उपयोगी जहां आप चाहते हैं कि code value set कर सके लेकिन कभी इसे वापस न पढ़ सके।

उदाहरण: Write-Only Classes

java
class Login {
	private String password;
	public void setPassword(String password) {
		this.password = password; // no getter: write-only
	}
}
public class Main {
	public static void main(String[] args) {
		Login login = new Login();
		login.setPassword("secret");
		System.out.println("Password set");
	}
}

Using the 'this' Keyword

एक setter के अंदर, parameter को अक्सर उस field जैसा ही नाम दिया जाता है जिसे यह update करता है, इसलिए this parameter (name) को object की अपनी field (this.name) से distinguish करता है — इसके बिना, name = name; बस parameter को खुद assign कर देता और field को कभी नहीं छूता।

उदाहरण: Using the 'this' Keyword

java
class Person {
	private String name;
	public void setName(String name) {
		this.name = name; // this.name is the field, name is the parameter
	}
}
public class Main {
	public static void main(String[] args) {
		Person p = new Person();
		p.setName("Bob");
		System.out.println("Name set");
	}
}
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 को this.field = field उपयोग करना चाहिए, इसलिए field update नहीं होती।
  2. बिना validation के एक setter जोड़ना, जो encapsulation का point खो देता है।
  3. Getters को गलत नाम देना, क्योंकि एक boolean getter conventionally isName() उपयोग करता है।

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.