1. An inner class is a class that is defined in another class.
Cause: A. An internal class method can access data in the scope where the class definition resides, including private data.
B. Inner classes can be hidden from other classes in the same package.
C. Using anonymous inner classes is convenient when you want to define a callback function and do not want to write a large amount of code.
2. Internal classes can access their own data fields, or they can access the data fields of the perimeter class object that created it.
3. An object of an inner class always has an implicit reference to the outer class object that created it. This reference is not visible in the definition of an inner class.
4. Settings for the perimeter class are generally set in the constructor of the inner class.
5. Only internal classes can be private classes, whereas regular classes can only have package visibility, or public visibility.
Instance Code
Test class
Import Javax.swing.joptionpane;public class Test {public static void main (string[] args) {Talkingclock c = new Talkingclock (1000,true); C.start (); Joptionpane.showmessagedialog (null, "Quit?") "); System.exit (0);}}
function Class
Import Java.awt.toolkit;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.util.date;import Javax.swing.timer;public class Talkingclock {private int interval;private Boolean beep;public Talkingclock (int interval,boolean beep) {this.interval = Interval;this.beep = beep;} public void Start () {ActionListener li = new Timeprinter (); Timer T = new timer (interval,li); T.start ();} public class Timeprinter implements Actionlistener{public void Actionperformed (ActionEvent event) {Date now = new Date (); System.out.println ("Present time is:" + now); if (beep) toolkit.getdefaulttoolkit (). Beep ();}}}
Output Results
My Java Learning Note (16) about the inner class (part 1)