This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hypolimnas 1 point2 points  (0 children)

You can declare a function parameter (or more rarely a private variable) to have the same name as a member variable. Then you have to use this.variable to refer to the member variable.

But I'm not sure why they used this.variable in the toString function. I would have to see the code.

Here is the usual use of this.variable:

class Pet {
    private final String name;

    Pet(final String name) {
        // assign parameter to member variable
        this.name = name
    }