+ 2
How to use "this" keyword & how it functions?
2 Respuestas
+ 5
this refers to the current object.
public class myClass{
int x,y;
void ads(int x,int y){
this.x=x;
this.y=y;
}}
here this.x means the variable x of method ads, its not the x of class.
hence , this.x=x; takes value of x that is passed to ads and set the varibale x of class to it.
0
//Answer for why we use this.color =c is explained below
class shades
{
String color;
shades(String color)
{
this.color=color; // if we dont use this statement it will consider the local string variable color so it won't assign. this is used for assigning local variable value to the outer variable
}
}
class main
{
public static void main (String[] args)
{
shades s=new shades ("Red");
System.out.println("color is "+s.color);
}
}