this and super keyword

this keyword in java

There can be a lot of usage of java this keyword. In java, this is areference variable that refers to the current object.

Usage of java this keyword

Here is given the 6 usage of java this keyword.

  1. this keyword can be used to refer current class instance variable.
  2. this() can be used to invoke current class constructor.
  3. this keyword can be used to invoke current class method (implicitly)
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this keyword can also be used to return the current class instance.

super keyword in java

The super keyword in java is a reference variable that is used to refer immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.

Usage of java super Keyword

  1. super is used to refer immediate parent class instance variable.
  2. super() is used to invoke immediate parent class constructor.
  3. super is used to invoke immediate parent class method.

Similarities between this and super

 Before seeing difference between this and super keywords in Java, let’s see some similarities between them

1) Both this and super are non static and can not be used in static context, which means you can not use this and super keyword inside main method in Java. Failing to do so will result in compiler error “non static variable this can not be referenced from static context”. Same is true for using super keyword inside main method.

 

2) Both this and super can be used in constructor chaining to call another constructor e.g. this()and super() respectively calls no argument constructor of child and parent class.

As shown in this example, we are first forwarding call from no argument constructor of B, to a constructor which accepts one String argument, which further call to super(“”), a call to super class, one argument constructor.

3) If used them inside constructor than this and super must be first statement, otherwise compiler will complain. Which means you can not call this() and super() from same constructor.

Super vs This in Java

Now we know how to use super and this keyword in Java, and comfortable with there intended use. One use of this keyword, which I didn’t shown here is that you can also use them inside Inner classes, they are quite handy to get reference of outer class in Java, Outer.this and Outer.super can be used to get current instance of Outer class and it’s parent in Java. Don’t forget to replace Outer, with the name of enclosing class. Now In short, here are main differences between this and super keyword in Java

1) this is used in context of the class you are working on, while super is used to refer current instance of parent class.

2) Every constructor by default calls super(), which is a call to no argument constructor of parent class, but you can still call another constructor either explicitly by using this() or super().