Monday, December 5, 2016

Write a JAVA program to give the example for ‘this’ operator. And also use the‘this’ keyword as return statement.

Aim: Write a JAVA program to give the example for ‘this’ operator. And also use the‘this’ keyword as return statement.


Program:

class A
int id;
void setId(int id)
{
this.id=id;
A getA()
{  
return this;  
}  
void msg()
{
System.out.println(id+" : "+"Hello");
}  
}  
  
class Test
{  
public static void main(String args[])
{  
A ob=new A();
ob.setId(1);
ob.getA().msg(); 
ob.setId(2);
ob.getA().msg();  
}  
}  

Output:


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home