Java internal class usage Summary

Source: Internet
Author: User

1. Definition

An internal class refers to defining another class within an external class.
An internal class is a member of an external class and is attached to an external class.
Internal classes can be static and can be modified using public, protected, and private. (The external class is not allowed: The External class can only use publi and default ).

2. Category
Internal class (member internal class) directly defined in a class (external class ),
Local internal class,
Static internal class,
Anonymous internal class.

3. Usage

A. Internal classes directly defined in a class

 

1) External classes cannot directly access members of internal classes, but can be accessed through internal class objects.

2) The internal class acts as a member of the external class and can access all members of the external class.

3) Note: Internal classes are a compilation concept. Once compiled successfully, they will become completely different. For an external class named outer and its internal defined internal class named inner. After compilation, the outer. Class and outer $ inner. Class classes are displayed.

4) The internal class of a Member is no different from that of a common member. It can be modified and restricted like a common member.

 Public   Class Mymain {

Int Outer_x = 100;

Class Inner {
// Bureau Variable. Its range is to define itsCodeBlock (not affected by modifier ).
Public Int Y = 10; // Bureau Partial variable
Private Int Z = 9;
Int M = 5;
// Static int K = 1; // Static variables cannot be defined in internal classes.

Public Void Display1 (){
System. Out. println ("display outer_x:" + outer_x );
}

Private Void Display2 (){
System. Out. println ("display outer_x:" + outer_x );
}
}

Void Test (){
Inner inner = New Inner ();

Inner. display1 ();
Inner. display2 ();

System. Out. println ("inner Y:" + inner. y ); // Accessible
System. Out. println ("inner Z:" + inner. z ); // Accessible
System. Out. println ("inner M:" + inner. m );// Accessible
}

Public Static Void Main (string ARGs []) {
Mymain outer = New Mymain ();
Outer. Test ();
}
}

B. Internal classes defined in a method

The internal class defined in the method is called a local internal class. Similar to local variables, the Public and Private modifiers are not added before the local internal class, and the range is the code block that defines it. Other usage is the same as that of the member internal class.

C. static internal class

Static internal class: non-static members of external classes cannot be accessed, which is limited by "static methods cannot directly access non-static members" in Java syntax. To access external class variables, you must use other methods. For this reason, static Nested classes are rarely used.

Static internal classes are actually separated from external classes. When creating static internal class objects, external class objects are not required. The essence is a common class placed inside another class. The static keyword only indicates that it does not depend on the existence of external class objects when creating an object. It does not mean that the class itself is static.

    • The static internal class syntax is basically the same as the preceding two non-static internal classes. The main difference is that the static keyword must be added before the declaration of the internal class. In addition, static internal classes cannot be defined using private.
    • Objects of static internal classes can be directly generated without external class objects. This actually makes static internal classes a top-level class. This is mainly because the static internal class is a static member of the external class and does not depend on the object of the external class.
    • The difference between static and non-static internal classes is essentially. A non-static internal class is a part of an external class object. It is mainly used to assist external class objects, and shares this relationship with external class objects.

D. Anonymous class

An anonymous internal class is a special local internal class, which has no class name. This class is applicable to classes that only use once and do not need to create objects multiple times. Using anonymous internal classes can make the class code and object creation complete at the same time. This is not only convenient, but also improves the maintainability of the Code.

    • An anonymous internal class is the only class without a constructor. Because there is no constructor, the use of anonymous internal classes is very limited.
    • Most anonymous internal classes are used for interface callback.
    • During compilation, the anonymous internal class is automatically named out $1. Class.
    • The anonymous internal class is used to inherit other classes or implement interfaces. No additional methods are required, but only the implementation or rewriting of the inherited methods.

Syntax rule: New <class or interface> <class subject>

 Interface PR
{
Void Print1 ();
}

Public Class Nonameclass
{
Public Pr dest ()
{
// This usage is used to create an instance of an object, override its function, and then return.
Return New Pr (){
Public Void Print1 (){
System. Out. println ("Hello world !! ");
}
};
}

Public Static Void Main (string ARGs [])
{
Nonameclass c = New Nonameclass ();
PR hW = C. DEST ();
HW. print1 ();
}
}

 

 

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.