What is super and this keyword in java?

keyword : this

‘this’ is used for pointing the current class instance. It can be used with variables or methods. Look into the following example:

class Test{
	private int i=10;
	public void m(){

In the above code this is used for the current instance. Since this is instance of a class it cannot be used inside a static method.

keyword : super

‘super’ is used for pointing the super class instance. See the following example.

class A
	int k = 10;
class Test extends A
	public void m()

In the above example the super keyword is used for accessing the super class variable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!