What is the difference between Super and This Keyword?

Answer Posted / deep

Super and This both are keywords and which has some predefined meaning..

Super - with the help of super, you could call super class constructor, method and member of super class..

This - it's used to avoid instance variable hiding, when local variable and instance variable has same name, then at time of assignment, local variable hides instance variable, so in order to get over this problem, this is used to refer instance variable..

class A {
int x; // instance variable
public void m(int x) {
x = x; // here local variable x hides instance variable
x.. so better use this to refer the instance variable

this.x = x; here this.x refer to instance variable and where another x is local variable
}

this is also used to call current class constructor and it can also used to pass current class constructor or method to another object..

you can also use this to call current class constructor or methods..

Rule: Call to this() must be the first statement in constructor or else it will result in compile time error..

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What happens when a thread cannot acquire a lock on an object?

543


What are the call back methods in entity bean?

566


What are local interfaces? Describe.

704


int x=5,i=1,y=0; while(i<=5) { y=x++ + ++x; i=i+2; } System.out.println(x); System.out.println(y); System.out.println(i); How to solve this? Please explain!

1369


hi friends, i have done my BE(CSE)at 2011 then i have joined one company before they asked me 50k so i paid but now they telling no project here so we r going to close like that so now i need any job i have good knowledge in core java and j2ee(jsp,servlet,jdbc) so if u know any job pl help me

1785






What is the relationship between an event-listener interface and an event-adapter class?

558


What are the different algorithms used for clustering?

554


Have you used threads in Servelet?

1935


When is the best time to validate input?

612


What method is invoked to cause an object to begin executing as a separate thread?

572


Why is actionform a base class rather than an interface?

574


To what value is a variable of the string type automatically initialized?

615


What is the purpose of the notify() method?

602


What is the relationship between the canvas class and the graphics class?

544


what are getters and setters in Java with examples?

1288