[Solved] Is there an alternative to using ‘this’ in this code?


would like to know how ‘this’ is being used

“this” refers to the current instance of a class.

if there is another way of doing it with same end result.

There is no need to explicitly create a Sequence variable in the append(…) method.

You can just invoke the add(…) method directly and return “this”:

    public Sequence append(Sequence other)
    {
        int i = 0;
        //Sequence a = this;

        while(i < other.values.size())
        {
            //a.add(other.values.get(i));
            add(other.values.get(i));

            i++;
        }

        //  return a;
        return this;

    }

Methods of the class always operate on the current instance of the class so there is no need to use “this” to get a reference to the class.

1

solved Is there an alternative to using ‘this’ in this code?