SE450: Java: this and super [8/41] Previous pageContentsNext page

in Java, this is a reference to the instance that is the receiving instance of a method call.

this is usually used in two ways:

  1. to pass the object instance as a parameter to another method
  2. to access instance fields that are shadowed
        public someMethod(SomeClass o) {
            o.hereIAm(this);
        }
        

You will see the second one in many setter methods

        public void setSomeField(String somefield) {
            this.somefield = somefield;
        }
        

this is just used to define scope for the variable

The keyword super represents an object reference having the same value as this, but it behaves as if it were a reference to the superclass.

It is used many times to call constructors. (more on this later)

Previous pageContentsNext page