Home | Java - The this Keyword

Chapter: Web or internet Programming : JAVA

Java - The this Keyword

Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked.

The this Keyword

 

Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked.

 

class box{

double      height;

double      depth;

 

double width;

 

// this operator & constructor when 3 dimensions are known box(double w, double d, double h){

 

this.width=w; this.height=h; this.depth=d;

}

 

//default constructor box (){

 

height=width=depth=2;

 

}

 

double fun_volume(){

return width*height*depth;

 

}

 

}

 

}

 

 

public class thisop {

 

public static void main(String args[]){

 

double volume;

 

box mybox1=new boxweight(10,10,10,10);

 

volume=mybox1.fun_volume();

System.out.println("volume of mybox1 object is " + volume);

 

}

}

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : JAVA : Java - The this Keyword |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.