My Java Learning Note (16) about the inner class (part 1)

Source: Internet
Author: User
Tags class definition date now

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)

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.