this keyword in java:- this keyword is a special reference variable that holds the object address and it gets created automatically when we create an object.
this keyword in java with Program
Example 1
public class A{
public static void main(String[] args) {
A a1 = new A();
a1.test();
}
public void test() {
System.out.println(this);
}
}
Output: A@2c7b84de
Example 2 – this keyword in Java program
public class A{
int x=100;
public static void main(String[] args) {
A a1 = new A();
a1.test();
System.out.println(a1.x);
}
public void test() {
System.out.println(this.x);
}
}
Output:
100
100
Note1: – Using this keyword we can access non-static members.
public class A{
int x=100;
public static void main(String[] args) {
A a1 = new A();
a1.test1();
System.out.println(a1.x);
}
public void test1() {
this.test2();
}
public void test2() {
System.out.println("Raju");
}
}
Output:
Raju
100
You Also Learn –
- What is Abstract Class in Java
- What is Lambda Expression in Java 8
- What is Exception Handling in Java
- Types of Exceptions in Java
- What is inheritance in Java
- What is the Final Keyword in Java
- What is Interface in Java
- What is Wrapper Class in Java
- Break Keywords in Java with Example
- What is Method In Java With Example
- What is Constructor in Java With Example
- What is Polymorphism in java
- What is a non-static variable in java
- Types of access specifiers in java
- What is the local variable in java
- Types of loops in java with program
- Remove duplicate elements in an Array in java
- What is var type in java
- What is a static variable in java
- What is the difference between return and return value?
Note2: – this keyword points to the current object running in the program.
public class A{
public static void main(String[] args) {
A a1 = new A();
a1.test();
A a2 = new A();
a2.test();
a1.test();
}
public void test() {
System.out.println(this);
}
}
Output:
A@2c7b84de
A@3fee733d
A@2c7b84de

Limitation of this keyword in java Program
- We can not use this keyword inside the static method to enhance the below program throws an error.
public class A{
public static void main(String[] args) {
System.out.println(this); //Error
}
}
2. using this keyword we can access non-static variables but we should not be done.
public class A{
int x=10;
public static void main(String[] args) {
A a1 =new A();
a1.test();
}
public void test() {
System.out.println(this.x);
}
}
Output: 10
3. Using this keyword we can access the static method ensured in the below program.
public class A{
public static void main(String[] args) {
A a1 =new A();
a1.test();
}
public void test() {
this.raj();
}
public static void raj() {
System.out.println("Learn Java");
}
}
Output : Learn Java
Uses of this() keyword in java with Program
- this() – it is used to call the constructor.
public class A{
A(){
System.out.println("Learn Java");
}
A(int x){
this();
}
public static void main(String[] args) {
A a1 = new A();
}
}
Output : Learn Java
Example 2
public class A{
A(){
this(10);
}
A(int x){
System.out.println(x);
}
public static void main(String[] args) {
A a1 = new A();
}
}
Output: 10
2. While calling the constructor using this() keyword ensure that this calling is being done from another constructor only. hence the below program throws an Error.
public class A{
A(){
System.out.println("Learn Java");
}
public static void main(String[] args) {
A a1 = new A();
a1.test();
}
public void test() {
this(); //Error
}
}
3. While calling a constructor using this() keyword it has to be always the first statement inside another constructor.
public class A{
A(){
System.out.println("Learn Java");
}
A(int x){
System.out.println(1000);
this(); // Error
}
public static void main(String[] args) {
A a1 = new A();
}
}