Java की this Keyword
In this page:
this.field = parameter;
this.methodName();
this(arguments); // call another constructor
Resolving Shadowed Instance Fields
जब एक constructor या method parameter एक instance field के साथ नाम share करता है, parameter उस block के अंदर locally field को shadow करता है।
this.name = name; लिखना दोनों को disambiguate करता है, parameter की value को explicitly object की field को assign करते हुए खुद को नहीं।
उदाहरण: Resolving Shadowed Instance Fields
class Person {
String name;
Person(String name) {
this.name = name; // disambiguates field from parameter
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice");
System.out.println(p.name);
}
}
Login to try C/C++/Java/PHP code in the editor
Passing 'this' as an Argument
आप this उपयोग करके current object को एक दूसरे method को एक argument के रूप में pass कर सकते हैं, जो तब handy है जब एक object को खुद का एक reference register या hand off करना हो, जैसे एक listener list में खुद को जोड़ना।
उदाहरण: Passing 'this' as an Argument
// Define the class Logger
class Logger {
void register(Registry r) {
r.add(this);
}
}
// Define the class Registry
class Registry {
void add(Logger l) {
// Print a line to the console
System.out.println("Registered: " + l);
}
}
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Logger object and store it in logger
Logger logger = new Logger();
logger.register(new Registry());
}
}
Login to try C/C++/Java/PHP code in the editor
Invoking Instance Methods
किसी दूसरे instance method के अंदर this.someMethod() call करना इसे explicitly current object पर invoke करता है; यह optional है क्योंकि Java अपने आप this imply करता है, लेकिन इसे लिखना visually clear कर सकता है कि call एक static या external वाले के बजाय एक local instance method को target करती है।
उदाहरण: Invoking Instance Methods
// Define the class Greeter
class Greeter {
void greet() {
this.sayHello();
}
void sayHello() {
// Print a line to the console
System.out.println("Hello!");
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
new Greeter().greet();
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor Delegation Chaining
एक constructor की पहली line के रूप में invoked this(...) उसी class में एक अलग overloaded constructor को delegate करता है, कई constructors को हर एक में setup code दोहराने के बजाय एक common initialization path share करने देते हुए।
उदाहरण: Constructor Delegation Chaining
// Define the class Box
class Box {
int size;
Box() {
this(10);
}
Box(int size) {
this.size = size;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Box object and store it in b
Box b = new Box();
// Print a line to the console
System.out.println(b.size);
}
}
Login to try C/C++/Java/PHP code in the editor
Returning 'this' for Method Chaining
एक instance method से this return करना current object का एक reference वापस देता है, जो method chaining enable करता है — एक fluent statement में कई methods को लगातार call करना, जैसे builder.setName("x").setAge(20).build()।
उदाहरण: Returning 'this' for Method Chaining
// Define the class Builder
class Builder {
// Create a new StringBuilder object and store it in sb
StringBuilder sb = new StringBuilder();
Builder add(String s) {
sb.append(s);
// Send this back to the caller
return this;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Builder object and store it in b
Builder b = new Builder();
b.add("Hello").add(" World");
// Print a line to the console
System.out.println(b.sb);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक static method के अंदर
thisउपयोग करना, जो एक compile error है। - एक constructor में
name = nameलिखना, जो parameter को खुद को assign करता है और field को unchanged छोड़ता है;this.name = nameउपयोग करें। - एक normal method से
this()call करना, क्योंकि यह सिर्फ एक constructor के पहले statement के रूप में काम करता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: