Java This Keyword - W3Schools
Maybe your like
❮ Java Keywords
Example
Using this with a class attribute (x):
public class Main { int x; // Constructor with a parameter public Main(int x) { this.x = x; } // Call the constructor public static void main(String[] args) { Main myObj = new Main(5); System.out.println("Value of x = " + myObj.x); } }Try it Yourself »
Definition and Usage
The this keyword refers to the current object in a method or constructor.
The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter). If you omit the keyword in the example above, the output would be "0" instead of "5".
this can also be used to:
- Invoke current class constructor
- Invoke current class method
- Return the current class object
- Pass an argument in the method call
- Pass an argument in the constructor call
Related Pages
Read more about objects in our Java Classes/Objects Tutorial.
Read more about constructors in our Java Constructors Tutorial.
Read more about methods in our Java Methods Tutorial.
❮ Java Keywords
★ +1 Sign in to track progressTag » What Does This Mean In Java
-
What Is The Meaning Of "this" In Java? - Stack Overflow
-
This Keyword In Java: What Is & How To Use With Example - Guru99
-
What Does This Mean In Java? - Linux Hint
-
What Does % Mean In Java? - Quora
-
What Does :: Mean In Java? - Tech With Maddy
-
Java - Basic Operators - Tutorialspoint
-
Using The This Keyword (The Java™ Tutorials > Learning The Java ...
-
This Keyword In Java: Meaning & Use
-
What Does ! Mean In Java With Examples
-
What Is += Addition Assignment Operator In Java? - DigitalOcean
-
What Is A Java Object? - Definition From Techopedia
-
This Keyword In Java - Javatpoint
-
What Is A Java String? - The Server Side