What is super and this keyword in java?

February 21, 2013

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.

email

Comments

comments