This keyword is used in Java to refer to the current instance of an object. Its specific functions include: referencing the current object instance calling other constructors from the constructor accessing members of the external class from the inner class
The role of this in Java
Short answer:
This keyword is used in Java to the current instance of the referenced object.
Detailed answer:
This keyword is a reserved word in Java, which can be used in methods, constructors and inner classes. Its main function is:
Example:
<code class="java">public class Example { private int x; public Example(int x) { this.x = x; } public int getX() { return this.x; } public static void main(String[] args) { Example example = new Example(10); int x = example.getX(); System.out.println(x); // 输出 10 } }</code>
In the above example:
Example(int x)
#this.x = x in ## assigns the parameter value to the object variable
x.
in method
getX() accesses object variable
x.
The above is the detailed content of The role of this in java. For more information, please follow other related articles on the PHP Chinese website!