Understanding the problem without this keyword, this keyword to refer current class instance variable.

Java Programming Language / Class, Object and Methods in java

1069

Program:

//Understanding the problem without this keyword
class StudentClass {

    int rollno;
    String name;
    float age;

    StudentClass(int rollno, String name, float age) {
        rollno = rollno;
        name = name;
        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. */

Output:

0 null 0.0
0 null 0.0
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.