My Java Learning Note (17) about the inner class (part 2)

Source: Internet
Author: User
Tags date now

1. The inner class is a compiler phenomenon that is independent of the virtual machine. The compiler will translate the inner class into a regular file that separates the external class name from the inner class, and the virtual machine knows nothing about it.

2. Internal classes have access privileges, so they are more powerful than conventional analogies.

3. Sometimes a class is used only once in one method, and we can define the class as a local class.

public void Start () {class Timeprinter implements Actionlistener{public void Actionperformed (ActionEvent event) {Date now = new Date (); System.out.println ("Current time is:" + now); if (beep) toolkit.getdefaulttoolkit (). Beep ();}} ActionListener li = new Timeprinter (); Timer T = new timer (interval,li); T.start ();}

4. A local class cannot be declared with the public or private access specifier, and its scope is scoped to declaring the local class block.

5. Local classes have an advantage in that they can be completely hidden from the outside world.

6. Compared with other internal classes, local classes also have an advantage. They can access not only the external classes that contain them, but also local variables. However, those local variables must be declared final.

7. If the local variable represents a dynamic counter, it can be converted to an array of length 1, the array variable is still declared final, but this simply means that it cannot refer to another array, the values in the array can be arbitrarily changed.

Final int[] C = new Int[1];for (int i = 0; i < dates.length;i++) {Dates[i] = new Date () {public int compareTo (Date other) { C[0]++;return Super.compareto (Other);}};}


8. Use the local inner class in one step further, if you create only one object of this class, you do not have to name it. This class is called an anonymous class.

New Supertype (construction parameters) {Inner class methods and data}

9. The name of the constructor must be the same as the class name. The anonymous class does not have a class name, so an anonymous class cannot have a constructor. Instead, the constructor arguments are passed to the superclass constructor. However, you cannot have any parameters when implementing an interface.

New InterfaceType () {Inner class methods and data}


10. The enclosing parenthesis of the construction parameter is followed by a curly brace, which is the anonymous inner class defined within the curly braces.

Instance Code

Test class

Import Javax.swing.joptionpane;public class Test {public static void main (string[] args) {Talkingclock c = new Talkingclock (); C.start (+, true); 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 {public void start (int interval,final Boolean beep) { ActionListener li = new ActionListener () {public void actionperformed (ActionEvent event) {Date now = new Date (); System.out.println ("Present time is:" + now); if (beep) toolkit.getdefaulttoolkit (). Beep ();}}; Timer T = new timer (interval,li); T.start ();}}


Output Results





My Java Learning Note (17) about the inner class (part 2)

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.