Introduction and differences about the usage of this and super in Java

Source: Internet
Author: User
Tags instance method

1.this&super

What is This,this is an object of its own, representing the object itself, which can be understood as: A pointer to the object itself. When you want to reference something in the current object, such as a method of the current object, or a member of the current object, you can use this for this purpose. Note that this can only be used in non-static methods in the class, and the static method and the static code block must never appear in this. His can also be used as a constructor function. You can see in the back

And what is super, can be understood as a pointer to a super (parent) class object, and this superclass refers to a parent class closest to itself. Super's role can also be used as a constructor, or to get the value of a variable of the same name for a parent object that is masked by a local variable.


2. Use as a constructor

Super (parameter): Invokes one of the constructors in the parent class (which should be the first statement in the constructor). This (parameter): Call the constructor of another form in this class (should be the first statement in the constructor).

A few key points to keep in mind are that this and super cannot coexist in the construction method, and that the next occurrence of this or super must be the 1th sentence of the construction method; The static method, which is the class method, cannot have this and the Super keyword, and the only thing that can coexist is in the instance method. Because this or super statement is not required to be the 1th sentence.

Look at this very classic example (because it is very instructive so it is copied directly from the Internet):

Class Person {public      static void prt (String s) {         System.out.println (s);      }        Person () {         prt (' A person. ');      } Construction method (1) Person         (String name) {         prt ("A person name is:" + name);      } Construction Method (2)  } public     class Chinese extends person {      Chinese () {         super ();//Call Parent class construction Method (1)         prt ("A Chinese. "); /(4)      }         Chinese (String name) {         super (name);//Call the parent class with the same formal parameter construction method (2)         prt ("His name is:" + name);      }         Chinese (String name, int age) {This         (name);//Call a construction method with the same formal parameter (3)         prt ("He is:" + age);      }         public static void Main (string[] args) {         Chinese cn = new Chinese ();         cn = New Chinese ("Kevin");         cn = New Chinese ("Kevin");      }  }  


The result of the execution is:
A person.
A Chinese.
A Person Name Is:kevin
His name Is:kevin
A Person Name Is:kevin
His name Is:kevin
His age is:22


3. Use this to represent the object reference of the currently calling method

Suppose you want to get a reference to the current object within the method, using the keyword this. The This keyword can only be used inside a method, representing a reference to the "object that called the method". See the example below
<span style= "FONT-SIZE:12PX;" >button Bn;...bn.addactionlistener (This);</span>


4. Call member variables and methods using this and super

There are many situations where you want to invoke a homogeneous or parent class member variable if you are using a local variable with the same name as a member variable. Look at the following example:

Class Person{public int c;private string name;private int age;protected void SetName (String name) {this.name=name;} protected void Setage (int age) {this.age=age;//Use this to avoid a local variable that obscures the class's member variable}protected void print () {System.out.println ("Name = "+name+" age= "+age);}} public class Demosuper extends person{public void print () {System.out.println ("Demosuper:"); Super.print ();//method of calling parent class} public static void Main (string[] args) {demosuper ds=new demosuper ();d s.setname ("Kevin");d S.setage ();d s.print ();}}
Output Result:

Demosuper:
Name=kevin age=22

Summarize the calling variables and methods:

For variables: This is used to access instance variables to differentiate local variables, and super is used to access the properties of the parent class (provided access control permission is allowed)
When used with methods: implicitly calls methods of this class, super is used to access the parent class's methods (provided access control permission is allowed)


The usage difference and summary of 5.this and super

1) Super (parameter): invokes the constructor method of the parent class.
2) This (parameter): Calls the other constructor methods of this class.
3) must be placed on the first line of the construction method.
3) Super: Refers to a member of a parent class, which is used when the name of a method in a child class and parent class is the same.
4) This: represents the current object name (which is prone to two semantics in the program, you should use this to indicate the current object, and if the function's shape participates in the class member data with the same name, this is required to indicate the member variable name).
5) This () and super () all refer to objects, so they cannot be used in a static environment. Includes: static variable, static method, static statement block.
6) In essence, this is a pointer to this object, but super is a Java keyword.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.