//Solution of the previous problem by this keyword
class StudentClass{
int rollno;
String name;
float age;
StudentClass(int rollno,String name,float age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
void display(){
System.out.println(rollno+" "+name+" "+age);
}
}
class MainStudentClass{
public static void main(String args[]){
StudentClass s1=new StudentClass(1,"Rahim",21.0f);
StudentClass s2=new StudentClass(2,"Ram",21.5f);
s1.display();
s2.display();
}
}
/*
The this keyword can be used to refer current class
instance variable. If there is ambiguity between the
instance variables and parameters, this keyword resolves
the problem of ambiguity.
In the previous example, parameters (formal arguments) and instance
variables are same. So, we are using this keyword to distinguish
local variable and instance variable.
*/
1 Rahim 21.0
2 Ram 21.5
Press any key to continue . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.