The difference between this and super in Java

Source: Internet
Author: User

This A reference to the object that represents the currently calling method:

(Who calls this method, who is the object, this is the reference to it)

Like what:

<pre name= "code" class= "Java" >public class this_demo{     String name;     public void SetName (String name) {       this.name = name;//The previous name is a member property of this class: name; the latter name is a parameter in the SetName.    }   public static void Main (String args[]) {         
           This_demo td=new This_demo ();
           Td.setname ("Zhangjilu");}   }

The TD above calls the SetName () method, so this is the reference to it.The Java keyword This can only be used in a method method body. When an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object that refers to itself, which is the name of this pointer.

This can only be used in non-static methods in a class, and this is not available in static methods and static code blocks.

Super key is similar to this action, which is a member variable or member method of a masked parent class or becomes visible.

The difference between this and super in Java

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.