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.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*