0% found this document useful (0 votes)
35 views4 pages

This Keyword PDF

This keyword refers to the current class instance and resolves ambiguity between instance variables and parameters. It distinguishes between local variables and instance variables when they have the same name. In the example, this keyword is used in the Student constructor to assign the parameter values to the instance variables with the same name, avoiding confusion between the two.

Uploaded by

dipesh mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

This Keyword PDF

This keyword refers to the current class instance and resolves ambiguity between instance variables and parameters. It distinguishes between local variables and instance variables when they have the same name. In the example, this keyword is used in the Student constructor to assign the parameter values to the instance variables with the same name, avoiding confusion between the two.

Uploaded by

dipesh mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

this Keyword

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.

Example 1
Program where this keyword is not required

class Student
{
int rollno;
String name;
float fee;

Student(int r,String n,float f)


{
rollno=r;
name=n;
fee=f;
}

void display(){System.out.println(rollno+" "+name+" "+fee);}


}
class TestThis3
{
public static void main(String args[])
{
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}
}

Example 2
Understanding the problem without this keyword
Let's understand the problem if we don't use this keyword by the example given below:

class Student
{
int rollno;
String name;
float fee;

Student(int rollno,String name,float fee)


{
rollno=rollno;
name=name;
fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis1
{
public static void main(String args[])
{
Student s1=new Student(111,"ankit",5000);
Student s2=new Student(112,"sumit",6000);
s1.display();
s2.display();
}
}
Example 3
In the above example, parameters (formal arguments) and
instance variables are same. So, we are using this keyword to
distinguish local variable and instance variable.

Solution of the above problem by 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();
}}

You might also like