Understanding the problem without "this" keyword
class Student{Solution of the above problem using "this" keyword
int id;
String name;
Student(int id, String name){
id = id;
name = name;
}
void display(){
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student s1 = new Student(101, "Jahn");
Student s2 = new Student(102, "Robin");
s1.display(); //0 null
s2.display(); //0 null
}
}
class Student{
int id;
String name;
Student(int id, String name){
this.id = id; //try with "this" keyword
this.name = name; //try with "this" keyword
}
void display(){
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student s1 = new Student(101, "Jahn");
Student s2 = new Student(102, "Robin");
s1.display(); s2.display(); }
}
Source: http://www.javatpoint.com/this-keyword
Check this link. It has all 6 types of examples using "this" keyword
This keyword in java
ReplyDeleteonly one word for this information awesome, keep sharing