this is a keyword in Java that you can use in your instance methods.
It always refers to the current instance that is executing the method.
is equivalent to this:
// We are using this to distinguish between number, the instance variable, and number, the method parameter
The example above is one use of this.
However, we’ll usually just go the first route, and choose parameter names that don’t conflict with our instance variable names.
This helps avoid mistakes.
checkstyle will help you with this, complaining (as it does above) that your parameter “hides a field”.
However, there is one place where we will use this.
Let’s go through it together:
static class methods are not uncommon.
Particularly when you have methods that don’t need an instance to function properly.
Let’s look at one example of a class that consists almost entirely of static methods,
You can also use the static keyword on fields.
One common use for this is to establish constants to make our code more readable and less error-prone.
To do this, we combine static with a keyword that we haven’t seen before, final.
final creates a variable that cannot be changed after it has been set.
We can combine static and final to create a symbolic constant.
Let’s look at how: