Program to illustrate the use of "THIS" keyword

in this tutorial we are learn about "THIS" keyword in java.
basically THIS keyword is reference variable in java, this keyword is use to reference the current object.
  • it can be used to refer instance variable of current classes.
  • it can be use to invoke current class constructor .
  • it can be passed as an argument in the method call.
  • it can be pass an argument in the constructor call.
  • it can be use to return the current class instance. 
//Program to illustrate the use of "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 std

 

{

 

public static void main(String[] args) {

 

student s1=new student(1951027,"Niriksha",6000f);

student s2=new student(1951013,"Bhagyashri",6000f);

 

s1.display();

 

s2.display();

 

}

}

 output:

Program to illustrate the use of "THIS" keyword

Post a Comment

0 Comments