Java Lect 12
Java Lect 12
Presented By
Dr. Sudipta Sahana
Asso. Prof.
Dept. of CSE
UEM - Kolkata
Topic of Interest
this keyword in java
this – to invoke current class method
this() – to invoke current class constructor
this – to pass as an argument in the method
this – to pass as argument in the constructor call
this keyword can be used to return current class instance
Methods returning objects
this keyword in java:
You may invoke the method of the current class by using the this keyword. If you don't
use the this keyword, compiler automatically adds this keyword while invoking the
.
method. Let's see the example:
class A{
void m(){System.out.println("hello m");}
void n(){
System.out.println("hello n");
m(); //same as this.m() Output –
//this.m(); hello n
} hello m
}
public class TestThis4{
public static void main(String args[]){
A a=new A();
a.n();
}}
this() – to invoke current class constructor
The this() constructor call can be used to invoke the current class constructor. It is used to
reuse the constructor. In other words, it is used for constructor chaining.
class A{
A(){System.out.println("hello a");} Output-
A(int x){
this(); hello a
System.out.println(x); 10
}
}
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}}
this – to pass as an argument in the method:
The this keyword can also be passed as an argument in the method. It is mainly
used in the event handling. Let's see the example
class S2{
void m(S2 obj){
System.out.println("method is invoked");
} Output -
void p(){
m(this);
} method is invoked
public static void main(String args[]){
S2 s1 = new S2();
s1.p();
}
}
this – to pass as argument in the constructor call
We can pass the this keyword in the constructor also. It is useful if we have to use one
object in multiple classes. Let's see the example:
class B{
A4 obj;
B(A4 obj){
this.obj=obj;
}
void display(){
System.out.println(obj.data);//using data member of A4 class Output -
}
} 10
class A4{
int data=10;
A4(){
B b=new B(this);
b.display();
}
public static void main(String args[]){
A4 a=new A4();
}
}
this keyword can be used to return current class instance
We can return this keyword as a statement from the method. In such case, return type of
the method must be the class type (non-primitive). Let's see the example:
class A{
A getA(){
return this;
}
void msg(){System.out.println("Hello java");} Output -
}
public class Test1{ Hello java
public static void main(String args[]){
new A().getA().msg();
/* A a = new A();
A b = a.getA();
b.msg(); */
}
}
this keyword as local variable suppressor
In show() method, the local variable x suppresses the instance variable x. the console
text output will be 10 and 940. “this” keyword refers to the current object, i.e. the object
that is invoking the method.
public class A
{int x=940;
void show(){
int x=10;
Output -
System.out.println(x);
System.out.println(this.x);} 10
public static void main (String []args){ 940
A a = new A();
a.show();
}
}
this keyword can be used to return current class instance
class A5{
void m(){
Output
System.out.println(this);//prints same reference ID
} A5@6d06d69c
public static void main(String args[]){ A5@6d06d69c
A5 obj=new A5();
System.out.println(obj);//prints the reference ID
obj.m();
}
}
Methods returning objects
In java, a method can return any type of data, including objects. For example, in the
following program, the incrByTen( ) method returns an object in which the value of a
(an integer variable) is ten greater than it is in the invoking object.
class ObjectReturnDemo{
int a;
ObjectReturnDemo(int i)
{
a = i;
}
// This method returns an object
ObjectReturnDemo incrByTen() {
ObjectReturnDemo temp = new
ObjectReturnDemo(a+10); Output
return temp; ob1.a: 2
}}
// Driver class ob2.a: 12
public class Test
{
public static void main(String args[])
{
ObjectReturnDemo ob1 = new ObjectReturnDemo(2);
ObjectReturnDemo ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
}}
Thank
You