×

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 TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Difference between print() and println() in Java

In this tutorial, we will discuss the differences between print and println in Java language. There are mainly two methods to display the text on the console printprintln The above two methods are...

4 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

How to sort an array in Java

Sorting is the process of arranging the elements of a list or array in a specific order, either ascending or descending. The sorting criterion numerical and alphabetical is commonly used...

6 minutes read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Virtual Function in Java

In the Operated Oriented Programming language, a virtual function or virtual method is a collection of functions that overrides the functionality of a function in an inheriting class with the same...

4 minutes read.