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

Java की this Keyword

this का मतलब है 'वह object जिसके अंदर मैं अभी हूं', me या my कहने जैसा। यह तब मदद करता है जब एक नाम दो चीज़ें मतलब कर सकता है और एक object को खुद के बारे में बात करने देता है।
Syntax
java
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

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

Passing 'this' as an Argument

आप this उपयोग करके current object को एक दूसरे method को एक argument के रूप में pass कर सकते हैं, जो तब handy है जब एक object को खुद का एक reference register या hand off करना हो, जैसे एक listener list में खुद को जोड़ना।

उदाहरण: Passing 'this' as an Argument

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

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

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

Constructor Delegation Chaining

एक constructor की पहली line के रूप में invoked this(...) उसी class में एक अलग overloaded constructor को delegate करता है, कई constructors को हर एक में setup code दोहराने के बजाय एक common initialization path share करने देते हुए।

उदाहरण: Constructor Delegation Chaining

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

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

java
// 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);
	}
}
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. एक static method के अंदर this उपयोग करना, जो एक compile error है।
  2. एक constructor में name = name लिखना, जो parameter को खुद को assign करता है और field को unchanged छोड़ता है; this.name = name उपयोग करें।
  3. एक normal method से this() call करना, क्योंकि यह सिर्फ एक constructor के पहले statement के रूप में काम करता है।

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.