-1
public class LinkedListNode {
    public LinkedListNode next;
    public LinkedListNode prev;
    public LinkedListNode last;
    public int data;
    public LinkedListNode(int d, LinkedListNode n, LinkedListNode p) {
        data = d;
        setNext(n);
        setPrevious(p);
    }
    
    public LinkedListNode(int d) {
        data = d;
    }   
    
    public LinkedListNode() { }

    public void setNext(LinkedListNode n) {
        next = n;
        if (this == last) {
            last = n;
        }
        if (n != null && n.prev != this) {
            n.setPrevious(this);
        }
    }       
    public void setPrevious(LinkedListNode p) {
        prev = p;
        if (p != null && p.next != this) {
            p.setNext(this);
        }
    }   
  • How did setNext and setPrevious work in this class?
  • What does a single "this" refer to in those cases?
  • And what was that if statement for?
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Hank
  • 1
  • 2
  • 1
    `this` refers to the instance you are presently in. So it is checking to see if for example, `p.next` is not the same instance via `p.next != this` – WJS Jul 19 '20 at 16:26
  • 1
    Does this answer your question? [When should I use "this" in a class?](https://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class) – Progman Jul 19 '20 at 16:27

3 Answers3

0

this refers to an object it is inside of. You can only use this inside of the class, for example this.next is the same as next inside of this class code. Consequently this is the object itself.

J Asgarov
  • 2,526
  • 1
  • 8
  • 18
0

this is a constant variable that points to the current class, so it basically refers to public class LinkedListNode in a way. The setNext method sets the next variable to n, then checks if the current class is the same class as the last variable, finally it checks if n isn't null and the prev variable of the n class isn't the current class, then calling setPrevious in the n class.

setPrevious sets the prev variable to p which would be the previous occurance of this which is now p in the n class, and then if the previous class reference isn't null and the next variable of the previous class reference isn't the now current class reference which was this, it calls setNext.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Chloe Dev
  • 271
  • 1
  • 8
0

this refers to the instance you are presently in. So it is checking to see if for example, p.next is not the same instance via p.next != this

Here is an example to demonstrate the concept.

Foo foo1 = new Foo();
Foo foo2 = new Foo();
System.out.println(foo1);
foo1.printInstance(foo1); // should be the same.
System.out.println(foo2);
foo1.printInstance(foo2);

public class Foo {
   public void printInstance(Foo foo) {
        System.out.println(this);
        System.out.println(this == foo ? "Equal" : "Not equal");
    } 
}

Prints something similar to the following:

Foo@7a81197d
Foo@7a81197d
Equal
Foo@5ca881b5
Foo@7a81197d
Not equal
WJS
  • 36,363
  • 4
  • 24
  • 39