Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 15 February 2017

this keyword in java

 this keyword

'this' is a reference variable that refers to the current objects or instance of any class. 'this' is a special keyword in java represent current class object.


this keyword in java



Usage of this keyword in java

There can be lots of usage of java this keyword.
  • this can be used to refer current class instance variable.
  • this() can be used to invoke current class constructor.
  • this can be used to invoke current class method(implicitly).
  • this can be passed as an argument in the constructor call.
  • this can be passed as an argument in the method call.
  • this can be used to return the current class instance from the method. 


1) this can be used to refer current class instance variable

this keyword can be used to refer the current class instance variable. If there is ambiguity between instance variables and formal parameters of a class, this keyword resolve this problem of ambiguity easily.


Problem without this keyword

class Students
{
int id;
String name;
Students(int id, String name)
{
id = id;
name = name;
}
void display()
{
System.out.println(id+" " +name);
}
public static void main(String args[])
{
Students s = new Students(1, "varun");
Students s1 = new Students(2, "rahul");
s.display();
s1.display();
}
}

output: 0 null
              0 null

This above example produce default values(0, null) of instance variables(data members) or objects because there is instance variable and parameters are same, so we are using this keyword to distinguish instance variable or parameters(local variables), so that we can achieve desired output.


Solution of the above problem by this keyword


class Students
{
int id;
String name;
Students(int id, String name)
{
this.id = id;
this.name = name;
}
void show()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Students s = new Students(1,"rahul");
Students ss = new Students(2,"manav");
s.show();
ss.show();
}
}

output: 1 rahul
              2 manav

There is no need of this keyword, If instance variables and parameters(local variables ) are different. For example


class Employees
{
int id;
String name;
Employees(int i, String n)
{
id = i;
name = n;
}
void show()
{
System.out.println(id+""+name);
}
public static void main(String args[])
{
Employees e = new Employees(100,"manav");
Employees ee = new Employees(101,"raghav");
e.show();
ee.show();
}


output: 100 manav
              101 raghav

2) this() can be used to invoke current class constructor 

this() which can be used to call one constructor within the another constructor without  creation of objects multiple times for the same class. In other word this() is used to constructor chaining. 

Syntax: 

this();//for calling default constructor
this(value1, value2....);//for calling parameterized constructor



Rules to use this()

this() always should be the first statements of the constructor. One constructor can call only other single constructor at a time by using this(). 

class Add
{
Add()
{
System.out.println(" default or no parameterized constructor");
}
Add(int i)
{
this();//calling default constructor
System.out.println(" One parameterized constructor");
}
Add(int i, int j)
{
this(10);//calling one parameterized constructor
System.out.println(" two parameterized constructor");
}
}
class thisDemo
{
public static void main(String args[])
{
Add a = new Add(20,30);
}
}

save Add.java
javac Add.java
java thisDemo

output: default or no parameterized constructor
             One parameterized constructor
             two parameterized constructor

you can't use this() in second statement or second line in any constructor.

For example:

class This
{
This()
{
System.out.println("default constructor");
}
This(int i)
{
System.out.println("parameterized constructor");
this();//compile time error
}
public static void main(String args[])
{
This t = new This(10);
}
}

output: compile time error: call to this must be first statement in constructor

3) this can be used to invoke current class method

by using this keyword you can invoke the method of the current class. If you do not use the this keyword, compiler automatically add this keyword at time of invoking of the method.

class A                                                                  class A
{                                                                            {
void b()                                                                 void b()
{   }                                                                       {   }
void c()                                                                  void c()
{                                                                             {
b();                               →            compiler    →       this.b();
}                                                                              }
psvm(String args[])                                              psvm(String args[])
{                                                                              {
new A().c();                                                            new A().c();
}                                                                               }
}                                                                               }


class A
{
void b()
{
System.out.println("this is b");
}
void c()
{
System.out.println("this is c");
b();//b() same as this.b(),no need to use this here compiler does it
}
public static void main(String args[])
{
A a= new A();
a.c();
}
}

output: this is c
             this is b

4) this can be passes as argument in method call

this keyword can be passed as an argument in method.

class Men
{
void a(Men m)
{
System.out.println("i am a");
}
void b()
{
System.out.println("i am b");
a(this);
}
public static void main(String args[])
{
Men m = new Men();
m.b();
}


output: i am b
              i am a

5) this keyword can be used to return current class instance

we can return this keyword as an statements from the method but return type of the method must be the class type.

class A
{
A getA()
{
return this;
}
void show()
{
System.out.println("this is show");
}
public static void main(String args[])
{
new A().getA().show();
}


output: this is show

6) Let's check this keyword that refer to current class instance variable or not

In this example, we are printing reference variable and this ,output of both variable will be same.

class A
{
void show()
{
System.out.println(this);
}
public static void main(String args[])
{
A obj = new A();
System.out.println(obj);
obj.show();
}


output: A@24273305
              A@24273305
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate