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(){
		System.out.println(this.i);
	}
}

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()
	{
		System.out.println(super.k);
	}
}

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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>