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

To identify IDL language what mapping mechanism is used?

3629


what is Activation Instantinator?

2096


Do we need to override service() method

1365


How can I scroll through list of pages like the search results in google?

753


What is prototype?

805






What is synchronization and why is it important?

727


Describe activation process?

2258


Which textcomponent method is used to set a textcomponent to the read-only state?

854


What are JTA/JTS and how they used by client?

1924


What must a class do to implement an interface?

750


Do I have to use jsps with my application?

744


What is a session? Can you share a session object between different theads?

742


Is the session factory thread safe?

890


What is the relationship between local interfaces and container-managed relationships?

806


What is TL and its use?

2191