Search code examples
linked-liststackthis

I want to know, in the code provided what does 'this' keyword referring to.?


/* I know this keyword is used to refer to class members but i am unable to understand what does 'this'.isEmpty() referring to. Some class ? class method? Or some variable?

For example: this.value = value; I understand that here, this.value is referring to class variable value but not for previous occurence of 'this'. */

public class StackWithMin extends Stack< NodeWithMin > {

public void push(int value) {

int newMin = Math.min(value, min()); super.push(new NodeWithMin(value,newMin));

}

public int min() {

if(this.isEmpty()) {

return Integer.MAX_VALUE; // Error value } else {

return peek().min;

}

} }

class NodeWithMin {

public int value;

public int min;

public NodeWithMin(int value, int min) {

this.value = v;

this.min = min;

}

}


Solution

  • "this" here is object of the class StackWithMin which is extending Stack class from java.util. so StackWithMin is instance of Stack class.

    this.isEmpty() which is method defined in Stack, here checks if the stack has any element or not. if it has zero element it returns true else false.

    Hope it clears your doubt.