Some discussions on internal and anonymous classes

Source: Internet
Author: User
Tags java anonymous class

Java Anonymous class and internal class.
Basic Theory:
-----------------------------------------------------
Java internal class: the definition of an internal class is defined within another class.

The reason is:

1. An internal class object can access the implementation of the object created for it, including private data. That is, an internal class instance is privileged to the instance of the class that contains it.

2. for other classes in the same package, the internal class can be hidden. In other words, the internal class, regardless of the method visibility, may be public. In addition to the inclusive class, other classes cannot use it.

3. Anonymous internal classes can easily define callbacks.

4. Use internal classes to easily write event-drivenProgram.

In fact, it is only intended to define callback-further, it is event-driven.

Interface and callback: a common programming mode is the callback mode. In this mode, you can specify the method of the callback object when a specific time occurs.
--------------------------------------------------
Note:
 
This in the anonymous class and internal class:
Sometimes, some internal and anonymous classes are used. When this is used in an anonymous class, this refers to the anonymous class or internal class itself.
If we want to use external class methods and variables, we should add the Class Name of the external class. For example:

/** An application to demonstrate the use of internal Classes */

/** Class Outer */
Class Outer
{
Private Static int size;
 
/** Internal class inner Declaration */
Public class inner
{
Private int size;

/** Method dostuff ()*/
Public void dostuff (INT size)
{
Size ++; // access local variables
This. Size ++; // access the member variables of its internal class
Outer. This. Size ++; // access the member variables of its external class
System. Out. println (size + "" + this. Size + "" + outer. This. size );
}
} // Internal class inner ends

/** Instance method testinner () method defined in class Outer */
Public void testinner ()
{
Inner I = new inner ();
I. dostuff (5 );
}

/** Main () method */
Public static void main (string [])
{
Outer o = new outer ();
O. testinner ();
}
} // Class outer ends

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.