this Keyword in Java
this is a keyword in Java which is used to hold the value current object(current object means the object whose method or constructor is being called).
this keyword cannot be used for any static function.
this keyword cannot be used for any static function.
Usage of this keyword:
1. The most common reason for using the this keyword is because a field is shadowed (read data shadowing below) by a method or constructor parameter.
2. We can also use the this keyword to call another constructor in the same class(Constructor chaining).
3. “this” keyword can be used as an argument in the method call.
What is meant by Data Shadowing?
Whenever a class level variable and a local variable both are having a same name then this concept is known as Data shadowing.
To understand the concept of data shadowing, consider this example:
To understand the concept of data shadowing, consider this example:
public class Demo
{
int x = 10;
int y = 20;
void show(int x, int y)
{
System.out.println("Value of x: " +x);
System.out.println("Value of y: " +y);
}
public static void main(String[] args)
{
Demo d = new Demo();
d.show(30,40);
}}
/* Output
value of x: 30
value of y: 40
*/
In this program, the show function has shadowed the class level variables x and y. and when we print the values inside the show function the local values will be printed.
But if we want to access the class level variables and print them inside the show function then we have to use “this” keyword.
1) Example of this keyword (Data shadowing with this concept):
public class Demo1
{
int x = 10;
int y = 20;
void show(int x, int y)
{
System.out.println("Value of x: " +this.x);
System.out.println("Value of y: " +this.y);
}
public static void main(String[] args)
{
Demo1 d = new Demo1();
d.show(30,40);
}}
/* Output
value of x: 10
value of y: 20
*/
2) Using “this” with a constructor
We can also use the “this” keyword to call another constructor in the same class with respect to the current constructor. This concept is known as constructor chaining or explicit constructor invocation.
See this example of constructor chaining:
class Demo
{
Demo(int x, int y)
{
this("Hello");
System.out.println((x+y));
}
Demo(int x)
{
this(10,20);
System.out.println(x);
}
Demo(String l)
{
System.out.println(l + " Java");
}
Demo()
{
this(10);
System.out.println("normal constructor");
}
public static void main(String[] s)
{
new Demo();
}}
/*Output
Hello java Function with constructor name
30
10
normal constructor
*/
In this program we are calling constructors from inside of other constructors by using this keyword.
this keyword as an argument in the methods
As we discussed that this keyword can also be used as an argument in the methods. Let us see an example of this keyword as an argument:
class Demo3
{
void view(Demo3 e)
{
System.out.println("Method invoked via this keyword");
}
void show()
{
view(this);
}
public static void main(String[] args)
{
Demo3 d = new Demo3();
d.show();
}
}
/* Output
Method invoked via this keyword
*/
No comments:
Post a Comment