SE450: Java: this and super [8/41] ![]() ![]() ![]() |
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:
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)