×

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest.

This keyword is used in Java.

  • this keyword is used six times in Java.
  • this can refer to the variable in the existing class instantiation.
  • this may be used to execute the existing class function (implicitly)
  • this() can be called the function Object()  of the current class.
  • this can be given as a result of a variety of arguments.
  • this function Object()  call can be provided as a parameter.
  • this could be employed to return the product's current class instance.

1) this: that refers to the existing class object instance

This keyword can be employed to refer to the existing class object instance. This phrase eliminates ambiguity among local variables and arguments.

Addressing the issue without all this keyword

Let's look at an example of what occurs if we don't utilize this search term:

Example: Students.java

class Students{  
int rollnos;  
String names;  
float fees;  
Students(int rollnos,String names,float fees){  
rollnos=rollnos;  
names=names;  
fees=fees;  
}  
void displays()
{
    System.out.println(rollnos+" "+names+" "+fees);
}  
}  
class TestThis1{  
public static void main(String args[])
{  
Students ss1=new Students(113,"amit",3000f);  
Students ss2=new Students(114,"sannit",4000f);  
ss1.displays();  
ss2.displays();  
}
}  

Output

0 null 0.0
0 null 0.0

The preceding example's arguments (formal parameters) and object properties are identical. As a result, we're utilizing this term to differentiate between local and instance variables.

Example:Students1.java

class Students1{  
int rollnos;  
String names;  
float fees;  
Students1(int rollnos,String names,float fees)
{  
this.rollnos=rollnos;  
this.names=names;  
this.fees=fees;  
}  
void displays()
{
    System.out.println(rollnos+" "+names+" "+fees);
}  
}  
class TestThiss2{  
public static void main(String args[]){  
Students1 ss1=new Students1(113,"amit",98000f);  
Students1 ss2=new Students1(114,"sannit",56000f);  
ss1.displays();  
ss2.displays();  
}
}  

Output

113 amit 98000.0
114 sannit 56000.0

There is no reason to use this term if variables (formal parameters) and example parameters are distinct, like in the following scheme:

This phrase is not necessary for this program.

Example: Students2.java

class Students2{  
int rollnos;  
String names;  
float fees;  
Students2(int r1,String n1,float f1){  
rollnos=r1;  
names=n1;  
fees=f1;  
}  
void displays()
{
    System.out.println(rollnos+" "+names+" "+fees);
    
}  
}  
  
class TestThis3{  
public static void main(String args[]){  
Students2 ss1=new Students2(101,"amit",50010f);  
Students2 ss2=new Students2(102,"sannit",6200f);  
ss1.displays();  
ss2.displays();  
}
}  

Output

101 amit 50010.0
102 sannit 6200.0

2) to call the current layer is responsible for managing

To use this keyword, one may call a method from the current group. If you do not include these keywords, the computer will add them for you when executing the procedure.

Example:A1.java

class A1{  
void method()
{
    System.out.println("hello students");
    
}  
void num()
{  
System.out.println("hello name");  
this.method();  
}  
}  
class TestThiss4{  
public static void main(String args[])
{  
A1 a1=new A1();  
a1.num();  
}
} 

Output

hello name
hello students

3) this(): invokes the current class's function Object().

To invoke the current class function Object(), use the this() function Object()  call. It allows you to reuse the function Object() . In other phrases, it serves to link constructors.

Using the customizable function Object() to call the default function Object():

Example:A1.java

class A1{  
A1(){System.out.println("hello anna");}  
A1(int x1){  
this();  
System.out.println(x1);  
}  
}  
class TestThiss5{  
public static void main(String args[]){  
A1 a1=new A1(101);  
}}  

Output

hello anna
101

Using the generalized function Object()  instead of the default function Object() :

Example:TestThiss6.java

class A11{  
A11(){  
this(50);  
System.out.println("hello anne");  
}  
A11(int x1)
{  
System.out.println(x1);  
}  
}  
class TestThiss6{  
public static void main(String args[]){  
A11 a1=new A11();  
}
}  

Output

50
hello anne

Actual implementation of the this() function Object()  call

The this() function Object()  call should be utilized to reuse the function Object()  from the function Object(). It preserves the chain among constructors, i.e., it is utilised for function Object()  concatenation. Let's look at an example of how to utilise this keyword.

Example: TestThiss7.java

class Students11{  
int rollnos;  
String names,courses;  
float fees;  
Students11(int rollnos,String names,String courses){  
this.rollnos=rollnos;  
this.names=names;  
this.courses=courses;  
}  
Students11(int rollnos,String names,String courses,float fees){  
this(rollnos,names,courses);// the constructor is used for recursing  
this.fees=fees;  
}  
void displays()
{
    System.out.println(rollnos+" "+names+" "+courses+" "+fees);
}  
}  
class TestThiss7{  
public static void main(String args[]){  
Students11 ss1=new Students11(111,"amit","javaProgramming");  
Students11 ss2=new Students11(112,"sannit","javaProgramming",60000f);  
ss1.displays();  
ss2.displays();  
}
}

Output

111 amit javaProgramming 0.0
112 sannit javaProgramming 60000.0  

Example:TestThiss8.java

class Students{  
int rollnos;  
String names,courses;  
float fees;  
Students(int rollnos,String names,String courses){  
this.rollnos=rollnos;  
this.names=names;  
this.courses=courses;  
}  
Students(int rollnos,String names,String courses,float fees){  
this.fees=fees;  
this(rollnos,names,courses);// returns the compile time error
}  
void display()
{
    System.out.println(rollnos+" "+names+" "+courses+" "+fees);}  
}  
class TestThiss8{  
public static void main(String args[]){  
Students ss1=new Students(111,"Suman","javaPrograming");  
Students ss2=new Students(112,"Rakesh","javaProgramming",6000f);  
ss1.display();  
ss2.display();  
}}  

Output

error: call to this must be the first statement in the constructor

4) to be passed as a parameter to the method

The term this can also be used as a parameter in a procedure. It is mostly used in event management.

Consider the following example:

Example: St2.java

class St2{  
  void method(St2 obj){  
  System.out.println("The method is used");  
  }  
  void p1(){  
  method(this);  
  }  
  public static void main(String args[]){  
  St2 ss1 = new St2();  
  ss1.p1();  
  }  
}  

Output

The method is used

This can be used as an argument in the following ways:

In event-driven (or) when we need to pass a reference to another class. It's employed to reuse a single object across many operations.

5) to be passed as a parameter in the function Object()  call

This keyword can also be sent to the function Object(). It comes in handy when we use the same object in numerous classes. Consider the following example:

Example:Al4.java

class B1{  
  A14 object;  
  B1(A14 object){  
    this.object=object;  
  }  
  void displays(){  
    System.out.println(object.da);// the data is displayed
  }  
}  
  
class A14{  
  int da=1098;  
  A14(){  
   B1 b1=new B1(this);  
   b1.displays();  
  }  
  public static void main(String args[]){  
   A14 a1=new A14();  
  }  
}  

Output

1098

6) This keyword may be used to return the present instance of the class

This keyword can be returned as a sentence by the procedure. In such a circumstance, the solution's function returns must be of the specific class (non-primitive).

Here's an example:

This grammar can be provided as just a declaration

return_type methodname(){  
return this;  
}  

Example of return type constructor

Example:Tests11.java

class A1{  
A1 getA1(){  
return this;  
}  
void msgs()
{
    System.out.println("Hi every one");
    
}  
}  
class Tests11{  
public static void main(String args[]){  
new A1().getA1().msgs();  
}  
}  

Output

Hi every one

Demonstrating this keyword

Let us demonstrate that this keyword relates to the property in the existing class instance. We're printing the insights regarding this program, and the result of both instances are identical.

Example:A15.java

class A15{  
void method(){  
System.out.println(this);// the reference id is then printed 
}  
public static void main(String args[]){  
A15 object=new A15();  
System.out.println(object);// the result is reference id 
object.method();  
}  
}  

Output

A15@5a07e868
A15@5a07e868

Related Topics

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

InputMismatchException in Java

What is InputMismatchException? One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is...

4 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Java String split() method

Java String split() method split current String against given regular expression and returns a char array. Syntax: public String split(String regex)                 public String split(String regex, int...

2 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.