Java के Getters और Setters
In this page:
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?
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
Read-Only Classes
Setters छोड़ना और सिर्फ getters प्रदान करना एक class को construction के बाद effectively read-only बनाता है: callers इसकी state inspect कर सकते हैं लेकिन कभी नहीं बदल सकते, जो simple immutable-style value objects बनाने का एक उपयोगी तरीका है।
उदाहरण: Read-Only Classes
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());
}
}
Login to try C/C++/Java/PHP code in the editor
Write-Only Classes
Reverse pattern — getters के बिना setters — एक write-only class बनाता है, एक password जैसी sensitive values रखने के लिए उपयोगी जहां आप चाहते हैं कि code value set कर सके लेकिन कभी इसे वापस न पढ़ सके।
उदाहरण: Write-Only Classes
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि एक setter को
this.field = fieldउपयोग करना चाहिए, इसलिए field update नहीं होती। - बिना validation के एक setter जोड़ना, जो encapsulation का point खो देता है।
- Getters को गलत नाम देना, क्योंकि एक
booleangetter conventionallyisName()उपयोग करता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: