Loading...

What is `this` keyword in Java? Explained with examples

What is `this` keyword in Java? Explained with examples

The keyword is a predefined word given by the compiler, which has some specific tasks. Let’s take an example of an “import” keyword where it imports packages that can be user-defined or through an in-built Java package so that we can use classes and methods from that package without using the import keyword. We just can’t write the name of the package. In Java, one must use the import keyword to use the packages.

All the letters in the keywords will be lowercase characters. Others include super, return, and type, throw, implement, access modifiers like public, private, and protected, conditional statements like switch, else, default, break, and continue, and data types like boolean, byte, short, int, char, float, double, and long. We have object-oriented programming in Java, so we treat everything as classes and objects, so for everything we implement as a class, it’s a blueprint, so we use this keyword to create a class. In this blog, we discuss the “this” keyword in Java.

What is this Keyword in Java?

‘this’ is a keyword used to represent the method belonging to that particular current object. Every class will have variables and methods. Variables can be local, static, or instance types from the superclass. For example, variables are declared within the class but outside of the method in order to access them, which can be done via an object, and we use this keyword to do so. 

Note that the super keyword is not the same as “this.” We use the super keyword to override methods with the same name in the superclass. 

Use of this keyword in Java

The ‘this’ keyword is used to represent the instance variables of the current object. So ‘this.l’ means it variable of current class X.

class X { int l = 35; void display() { System.out.println( "so the number is ", l); // it is same as by default this.l } public static void main(String args[]) { X obj = new X(); obj.display(); } }
Code language: Java (java)
so the number is 35
Code language: Bash (bash)

In C programming, the global variable and local variable share the same value, and preference is given to the local variable, but in Java, we can display the local variables and instance variables using this keyword.

class XDemo { int l = 35; void display() { int l = 7; System.out.println("so the number is " + l); } public static void main(String arg[]) { XDemo obj = new XDemo(); obj.display(); } }
Code language: Java (java)
so the number is 7
Code language: Java (java)

So the value of the local variable is getting printed, not the instance variable. If both the variables have the same name and display function can accept both variables so by default it’s gonna take the local variable.

this: to refer current class instance variable

Here we used this keyword, So by using the ‘this’ keyword, we can print the instance variable, which is a representation of the current object.

class XDemo { int l = 35; void display() { System.out.println("The value of l is: " + this.l); } public static void main(String arg[]) { XDemo obj = new XDemo(); obj.display(); } }
Code language: Java (java)
The value of l is: 35
Code language: Bash (bash)

this: to invoke the current class method

class XDemo { void display() { System.out.println("This is the display method"); } void show() { this.display(); } public static void main(String arg[]) { XDemo obj = new XDemo(); obj.show(); } }
Code language: Java (java)

Output:

"This is the display method"
Code language: Bash (bash)

So we use this keyword to display the method in another method in the same class.

this(): to invoke current class constructor call()

class XDemo { int l; XDemo() { this(5); } XDemo(int l) { this.l = l; } public static void main(String arg[]) { XDemo obj = new XDemo(); System.out.println("The value of l is: " + obj.l); } }
Code language: Java (java)
"The value of l is: 5"
Code language: Bash (bash)

this: to pass as an argument in the method

class XDemo { void display(XDemo obj) { System.out.println(obj); } public static void main(String arg[]) { XDemo obj1 = new XDemo(); XDemo obj2 = new XDemo(); obj1.display(obj2); } }
Code language: Java (java)

Output:

XDemo@3b22cdd0
Code language: Java (java)

As you can see, the class name is “XDemo,”  and the hashcode is “3b22cdd0.” This output is produced by the line “System.out.println(obj);” in the display method, where “obj” is the object passed as an argument to the method.

this: to pass as an argument in the constructor call

class XDemo { int l; XDemo() {} XDemo(XDemo obj) { this.l = obj.l; } public static void main(String arg[]) { XDemo obj1 = new XDemo(); obj1.l = 5; XDemo obj2 = new XDemo(obj1); System.out.println("The value of l in obj2 is: " + obj2.l); } }
Code language: Java (java)

Mainly, we use the ‘this’ keyword for constructor chaining so that it looks simple and reduces the code. Here it is used as an argument in the constructor call.

The value of l in obj2 is: 5
Code language: Bash (bash)

this keyword can be used to return the current class instance

class XDemo { int l; XDemo() { this(5); } XDemo(int l) { this.l = l; } public static void main(String arg[]) { XDemo obj = new XDemo(); System.out.println("The value of l is: " + obj.l); } }
Code language: Java (java)
The value of l is: 10
Code language: Bash (bash)

We can also use it to return the current class instance where we used the setL method.

Conclusion

We used “this” six times: once to pass an argument to the method and once to constructors. It represents the current object for accessing the class objects, also avoiding name conflicts between variables and methods. When the same name is used for the instance variable and a local variable, it must be required which helps to reduce the code and is also used in Constructor chaining. In Java, it is referred to as instance variables, and it invokes class methods and constructors, passing an argument in methods and constructors. 

Frequently Asked Questions to Resolve(FAQs)

What is this keyword in Java?

“this” is a keyword used to represent the method belonging to that particular current object.

How to use this function in Java?

In Java, it is referred to as instance variables, and it invokes class methods and constructors, passing an argument in methods and constructors. 

What are the six ways to use this keyword?

We used “this” six times: once to pass an argument to the method and once to constructors. It represents the current object for accessing the class objects, also avoiding name conflicts between variables and methods. In Java, it is referred to as instance variables, and it invokes class methods and constructors, passing an argument in methods and constructors. 

What is the essence of this keyword in Java?

“this” refers to the current class members, including variables and methods. When the same name is used for their instance variable and a local variable, it must be required. helps to reduce the code and is also used in Constructor chaining.

How can this keyword be used in six different ways?

We discussed the “this” keyword in all six different ways in this blog, so read and practice carefully. Don’t confuse it with a super keyword; this helps in reducing the code.

Sharing is caring

Did you like what Nithin Reddy wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far