java / beginner
Snippet
The 'this' Keyword
The 'this' keyword refers to the current instance of the class. It is commonly used to distinguish between class fields and parameters with the same name.
snippet.java
1
2
3
4
5
6
class Person {String name;Person(String name) {this.name = name;}}
Breakdown
1
String name;
Declares an instance variable named 'name'.
2
this.name = name;
Assigns the parameter 'name' to the instance variable 'name' using 'this'.