Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this keyword doesn't represent an object

Tags:

java

Does anyone know why can we loop through the "this" keyword here (in the subsetOf method)? To my knowledge this represents a JAVA object. Some extensive explanations are welcomed, would like to know why "this" can work in this way.

public class ArrayListSet<E> implements Set<E> {

    private ArrayList<E> elements;

    public ArrayListSet() {
        elements = new ArrayList<>();
    }

    @Override
    public void add(E e) {
        if (!elements.contains(e))
            elements.add(e);
    }

    @Override
    public void remove(E e) {
        elements.remove(e);
    }

    @Override
    public boolean contains(Object e) {
        return elements.contains(e);
    }

    @Override
    public int size() {
        return elements.size();
    }

    @Override
    public boolean subsetOf(Set<?> other) {
        for (E e : this) {
            if (!other.contains(e))
                return false;
        }
        return true;
    }
}
like image 899
Stephen Fong Avatar asked Aug 21 '19 05:08

Stephen Fong


People also ask

Can we use this keyword in object?

The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object. Alone, this refers to the global object. In a function, this refers to the global object.

What type of function doesn't have its own this keyword?

Arrow functions don't have their own this […] binding. Instead, [this identifier is] resolved in the lexical scope like any other variable. That means that inside an arrow function, this [refers] to the [value of this ] in the environment the arrow function is defined in (i.e. “outside” the arrow function).

What is the this keyword in Java?

The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).

Which does not support this keyword in Java?

Output. The "this" keyword is used as a reference to an instance. Since the static methods doesn't have (belong to) any instance you cannot use the "this" reference within a static method. If you still, try to do so a compile time error is generated.


1 Answers

You are declaring a class which implements the Set interface which itself extends the Iterable through the Collection interface. Any Object which is an implementation of the Iterable interface can be used inside a for-each loop.

So the inheritance hierarchy is this for your class:

 Iterable
    ^
    |
Collection
    ^
    |
   Set
    ^
    |
ArrayListSet

Also the this keyword always refers to the current instance. In your code when you execute the subsetOf() the this will point to an instance of ArrayListSet (which was used to invoke the subSetOf() method) which is a type of Iterable through inheritance, so you can use it in a for-each loop.

Also since you are implementing a Set you need to supply an implementation of the Iterator<E> iterator(); method defined in the Set interface to make it an Iterable.

As implementations of the Iterable interface requires a iterator() method which returns an instance of the Iterator object. The Iterator Object will be used by the for-each loop to iterate over the elements of the ArrayListSet.

The implementation of the Iterator type returned from the iterator() method actually defines the iteration logic used by the for-each loop.

To use any Object in a for-each loop:

Step 1: Extend Iterable directly or through other Iterable types.

Step 2: Provide implementation of the Iterator iterator() method inherited from the Iterable interface.

Step 3: Implement an Iterator and return its instance from the iterator() method.

like image 60
Fullstack Guy Avatar answered Oct 06 '22 01:10

Fullstack Guy