You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Programme without this keywordclassSample{
inta, b;
Sample(inta, intb){ // constructora = a;
b = b;
}
voiddisplay(){
System.out.println("a is " + a + " b is " + b);
}
}
classDemo{
publicstaticvoidmain(Stringargs[]){
Sampleobj = newSample(100, 200); // object of class Sampleobj.display();
} // Output: a is 0 b is 0
}
In the above example, data members of a class are not initialized because the name of local variable and data members are same. To solve the above problem, this keyword is used
this is a keyword created by JVM and supplied to each and every java program
this keyword is used to point current class object whenever the formal parameter and data member of the class are same
// Programme with this keywordclassSample{
inta, b;
Sample(inta, intb){ // constructorthis.a = a;
this.b = b;
}
voiddisplay(){
System.out.println("a is " + a + " b is " + b);
}
}
classDemo{
publicstaticvoidmain(Stringargs[]){
Sampleobj = newSample(100, 200); // object of class Sampleobj.display();
} // Output: a is 100 b is 200
}