इस आर्टिकल में हम जावा this keyword क्या हैं | this keyword in Java in Hindi के बारे में जानेंगे।
this keyword क्या हैं? | this keyword in Java in Hindi
this कीवर्ड का जावा में बहुत उपयोग हो होता है। जावा में, यह एक reference variable है जो current object को refers करता है।
Usage of Java this keyword in Hindi | जावा इस कीवर्ड के उपयोग
यहाँ जावा this keyword के 5 उपयोग दिए गए हैं।
- this का उपयोग current class instance variable को refer करने के लिए किया जा सकता है।
- this का उपयोग current class method (implicitly) को लागू करने के लिए किया जा सकता है
- this() का उपयोग current class के constructor को invoke करने के लिए किया जा सकता है।
- this method कॉल में argument के रूप में passed किया जा सकता है।
- this का उपयोग current class के instance को method से return करने के लिए किया जा सकता है।
1) this: to refer current class instance variable | this का उपयोग current class instance variable को refer करने के लिए किया जा सकता है।
this keyword का उपयोग current class instance variable को refer करने के लिए किया जा सकता है। यदि instance variables और parameters के बीच ambiguity है, तो this कीवर्ड ambiguity की समस्या को हल करता है।
this कीवर्ड के बिना समस्या को समझना
यदि हम this keyword का उपयोग नहीं करते हैं तो समस्या को नीचे दिए गए उदाहरण से समझते हैं।
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
- Java में Method क्या हैं?| What is Method in Java in Hindi
- Objects and Classes in Java in Hindi
- Java Naming Convention in Hindi
- JavaScript क्या है? | what is javaScript in hindi? | advantage javaScript in hindi
Output:
111 ankit 5000.0
112 sumit 6000.0
variables के लिए meaningful names का उपयोग करना बेहतर तरीका है। इसलिए हम real time में variables और parameters के instance के लिए समान नाम का उपयोग करते हैं, और हमेशा this कीवर्ड का उपयोग करते हैं।
2) this: to invoke current class method | 2)this :current class method को invoke करने के लिए
आप this कीवर्ड का उपयोग कर current class के method का invoke कर सकते हैं। यदि आप this कीवर्ड का उपयोग नहीं करते हैं, तो compiler automatically रूप से method को invoke करते समय this कीवर्ड को जोड़ता है। आइए उदाहरण देखें।
class A{
void m(){System.out.println("hello m");}
void n(){
System.out.println("hello n");
//m();//same as this.m()
this.m();
}
}
class TestThis4{
public static void main(String args[]){
A a=new A();
a.n();
}}
Output:
hello n
hello m
3) this() : to invoke current class constructor | this(): current class के constructor को invoke करने के लिए
this() constructor call का उपयोग current class constructor को कॉल करने के लिए किया जा सकता है। इसका उपयोग constructor का reuse उपयोग करने के लिए किया जाता है। दूसरे शब्दों में, इसका उपयोग constructor chaining के लिए किया जाता है।
class A{
A(){System.out.println("hello a");}
A(int x){
this();
System.out.println(x);
}
}
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}}
Output:
hello a
10
4) this: to pass as an argument in the method | this: method में argument के रूप में pass करने के लिए
this कीवर्ड method में argument के रूप में भी passe किया जा सकता है। यह मुख्य रूप से event handling में उपयोग किया जाता है। आइए उदाहरण देखें:
class S2{
void m(S2 obj){
System.out.println("method is invoked");
}
void p(){
m(this);
}
public static void main(String args[]){
S2 s1 = new S2();
s1.p();
}
}
Output:
method is invoked
5) this: to pass as argument in the constructor call | this: constructor कॉल में argument के रूप में पारित करने के लिए
हम this कीवर्ड को constructor में भी पास कर सकते हैं। यह उपयोगी है अगर हमें एक object को कई classes में उपयोग करना है। आइए उदाहरण देखें:
class B{
A4 obj;
B(A4 obj){
this.obj=obj;
}
void display(){
System.out.println(obj.data);//using data member of A4 class
}
}
class A4{
int data=10;
A4(){
B b=new B(this);
b.display();
}
public static void main(String args[]){
A4 a=new A4();
}
}
OutPut
10
6) this keyword can be used to return current class instance | this कीवर्ड का उपयोग current class के instance को return करने के लिए किया जा सकता है Read More
return_type method_name(){
return this;
}
- what is loops statement in java in Hindi |जावा लूप क्या हैं
- difference between java and javascript in Hindi
- difference between java and javascript in Hindi
- Similarities and Difference between Java and C++ in hindi | C++ vs Java
निष्कर्ष
आशा करता हूँ कि आज के इस आर्टिकल में आपको this keyword क्या हैं | this keyword in Java in Hindi से सम्बंधित आपके सारे आंसर मिल गए होंगे।
आज के इस आर्टिकल में हमने जाना कि this keyword क्या हैं | this keyword in Java in Hindi इस आर्टिकल से संबधित आपका कोई प्रश्न हैं तो आप comment करें में आपकी कमेंट का जबाब जरूर दूंगा।
ऐसे ही IT, Computer Science से रिलेटेड जानकारियाँ पाने के लिए हमारे इस वेबसाइट को सब्सक्राइब कर लीजिये | जिससे आपको हमारी आने वाले नये आर्किल की notification प्राप्त हो सके।
अगर जानकारी अच्छी लगी हो तो अपने दोस्तों और class mate के साथ ये जानकारी जरूर शेयर करें जिससे उनकी भी हेल्प हो सके।
FAQ प्रश्न-
-
this keyword क्या हैं? | this keyword in Java in Hindi
this कीवर्ड का जावा में बहुत उपयोग हो होता है। जावा में, यह एक reference variable है जो current object को refers करता है।