What is this keyword in java best program

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 –

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
this keyword in java
this keyword in java

Limitation of this keyword in java Program

  1. 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

  1. 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();
	
	}
}

Leave a Comment