This pointer in Java uses methods to share _java

Source: Internet
Author: User

Java, generally speaking this pointer refers to the object of this code that is currently being accessed, but if you need to use an object in an external class in an inner class, you need to qualify with the class name of the outer class. This approach is also more common in Android development.

  @Author: Twlkyao

package Twlkyao;

public Class A {public 
 a () { 
  Inner Inner = new Inner ();
  Inner.outer (); Call the inner class ' s outer method.
  This.outer (); Call A ' s outer method.
 } 
 
 public void outer () { 
  System.out.println ("outer run");
 } 

 Class Inner {public
  void outer () {
   System.out.println ("Inner Run");
   A.this.outer (); Call A ' s outer method.
   System.out.println ("--------");
  }
 
 public static void Main (string[] args) {
  A A = new A ();
 }
}

Inner is an inner class that accesses the outer () method in Class A and, because of the same method in the anonymous inner class, needs to be qualified with the this pointer of a.

The output results are:

Inner run

outer run

--------

outer Run


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.