Inner class-access$000 Access private domain

Source: Internet
Author: User
Tags class definition

Inner class:

 PackageInnerclass;Importjavax.swing.*;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjava.util.Date;/*** Created by admin on 2017-3-29.*/ Public classInnerclasstest { Public Static voidMain (string[] args) {Talkingclock clock=NewTalkingclock (1000,true);        Clock.start (); Joptionpane.showmessagedialog (NULL, "Quit program?"); System.exit (0); }}classtalkingclock{Private intinterval; Private Booleanbeep;  PublicTalkingclock (intIntervalBooleanbeep) {         This. Interval =interval;  This. Beep =beep; }     Public voidstart () {ActionListener listener=NewTimeprinter (); Timer T=NewTimer (Interval,listener);    T.start (); }classTimeprinterImplementsactionlistener{@Override Public voidactionperformed (ActionEvent e) {System.out.println (NewDate ()); if(beep) {System.out.println ("Beep"); }    }}}

Non-intrinsic class:

 PackageInnerclass;Importjavax.swing.*;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjava.util.Date;/*** Created by admin on 2017-3-29.*/ Public classInnerclasstest { Public Static voidMain (string[] args) {Talkingclock clock=NewTalkingclock (1000,true);        Clock.start (); Joptionpane.showmessagedialog (NULL, "Quit program?"); System.exit (0); }}classtalkingclock{Private intinterval; Private Booleanbeep;  PublicTalkingclock (intIntervalBooleanbeep) {         This. Interval =interval;  This. Beep =beep; }    Static Booleanaccess$000(Talkingclock talkingclock) {returnTalkingclock.beep; }     Public voidstart () {ActionListener listener=NewTimeprinter ( This); Timer T=NewTimer (Interval,listener);    T.start (); }}classTimeprinterImplementsactionlistener{PrivateTalkingclock outer;  Publictimeprinter (talkingclock clock) {outer=clock; } @Override Public voidactionperformed (ActionEvent e) {System.out.println (NewDate ()); if(talkingclock.access$000(outer)) {System.out.println ("Beep"); }    }}

1. The inner class can access the data in the scope of the class definition, including the private. Because an object of an inner class has an implicit reference, he points to the outer class object that created it. The reference to the outer class is set in the constructor, and the constructor's argument is an external class reference.
2. Internal Class A compiler phenomenon, independent of the virtual machine. The compiler translates the inner class into a regular file that separates the external class name from the internal class name with $ (dollar sign). In order to refer to an external class, the compiler generates an additional instance domain this$0 (the name is a compiler composite and cannot be referenced in its own code).

3. The inner class is translated into a generic class with a strange name, and for the domain, each domain automatically generates a static method access$000 (or access$0) that accepts the owning class object as a parameter and returns the value to the domain. You can take advantage of this private method of calling Class A in class B.

Inner class-access$000 Access private domain

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.