×

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

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.