.


Divya

Java This keyword


this keyword:

  1. this is a reference variable that refers to the current object .
  2. this() can be used to invoke current class constructor.
  3. this keyword= invoke current class method
  4. It can be passed as argument in method call
  5. Argument in constructor call
  6. It can be used to return the current class instance.

Example by Using this keyword

class Student{
int rollno;
String name;
float school fees;
Student(int rollno,String name,float school fees){
this.rollno=rollno;
this.name=name;
this.school fees=school fees;
}
void display(){System.out.println(rollno+" "+name+" "+school fees);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(220,"Divya",10000f);
Student s2=new Student(330,"Mitra",20000f);
s1.display();
s2.display();
}}

Answer:

220 Divya 10000
330 Mitra 20000

Example by not using this keyword

class Student{ int rollno; String name; float school fees; Student(int r,String n,float s.f){ rollno=r; name=n; school fees=s.f; } void display(){System.out.println(rollno+" "+name+" "+school fees);} } class TestThis3{ public static void main(String args[]){ Student s1=new Student(220,"Divya",10000f); Student s2=new Student(330,"Mitra",20000f); s1.display(); s2.display(); }}

Answer:

220 Divya 10000
330 Mitra 20000



.