Some knowledge about the inner class of Java

Source: Internet
Author: User
Tags class definition

member Inner class:
Equivalent to a non-static member of a class, which can be decorated with permission modifiers, including private, protected, public.
1. Define the member inner class
Class Outter {
Non-static inner class
Class Inner {
Inner class member
int i = 12;
}
Ordinary members of an external class
int j = 0;
}
2. Create a member inner class object within the outer class
The syntax is the same as the normal creation object, and the new operator is used to invoke the appropriate construction method. Note that non-static inner classes are non-static members of external classes and cannot be used in static contexts.
Example:
Class Outter {
Non-static inner class
Class Inner {
Inner class member
int i = 12;
public void Innertest () {
System.out.println ("Inner Class Method");
}
}
Ordinary members of an external class
int j = 0;
public void Test () {
Inner Inner = new Inner ();
Inner.innertest ();
}
}
3. Create a member inner class object outside the outer class
Since it is a non-static member of an external class, it must be used in cases where the external class object exists.
The basic syntax is
< external class class name >.< inner class class name > Reference variable name = < reference to external class object >.new < inner class constructor >;
< external class class name >.< inner class class name > Reference variable name = new < external class constructor >.new < inner class constructor >;
(1), when creating an inner class object, the object reference must be < external class class name >.< inner class class name >
(2), calling the constructor of the inner class cannot be called directly with new but with reference to the < external class object >.new
Example:
Class Outter {
Non-static inner class
Class Inner {
Inner class member
int i = 12;
public void Innertest () {
System.out.println ("Inner Class Method");
}
}
Ordinary members of an external class
int j = 0;
}
Class Maintest {
public static void Main (string[] args) {
Outter outter = new Outter ();
Outter.inner Inner = Outter.new Inner ();
Inner.innertest ();
}
}
When accessing an inner class outside of an external class, you need to be aware of the restriction of the permission modifier, which is the same as a class member.
3. The. class file name format generated after the internal class compilation is the < outer class class name >$< inner class class name >.

4. The members of the inner class and the external class access each other
An inner class can access any member of an external class, including private members.
The external class accesses the members of the inner class to create an object of the inner class, which can then access any member of the inner class, including private members, and it is important to note that the member inner class cannot have static members.
When the members of an external class and members of an inner class have the same name, this alone is not distinguishable. Accessing the members of an external class in an inner class can be distinguished by the following syntax
< external class class names >.this.< member names that need to be accessed in an external class >;

Local inner class
The inner class definition becomes a local inner class in the method and is only locally valid. The class is serviced only for the method block in which it resides.
Local inner classes and member inner classes can access all members of the perimeter class, but cannot access ordinary local variables that are in a local block. This local variable is declared final if it is to be accessed.
The normal local variable dies after the block ends, and the local inner class object created does not die with the end of the statement block. The final local variable is stored in a different way from the normal local variable, and it will not disappear because of the end of the statement block, and it can be accessed by the local inner class.
Example:
public class InnerClassTest1 {
public static void Main (string[] args) {
Outter outter = new Outter ();
Forinner Forinner = Outter.getinner ();
Forinner.sayhello ();

}
}
Class Outter {
Forinner Forinner;
Public Forinner Getinner () {
Class Inner implements Forinner {
public void SayHello () {
System.out.println ("Hello, I am the local inner class object, I still exist!") ");
}
}
Forinner = new Inner ();
return forinner;

}
}
Interface Forinner {
void SayHello ();
}
Because the local inner class is only locally valid, you cannot use the local inner class's reference outside to point to the object of the local inner class, you can only implement the interface with the local inner class and create the local inner class object, and on the outside, point to the local inner class object with the interface reference.
Local inner classes in static methods can access only static members of the perimeter class, not non-static members.
The. class file name generated by the local inner class is the < outer class class name >$<n>< inner class class name > where n is the inner class of the part

Static Inner class
Definition of static inner class
Class Outter {
Static Class Inner {
/****/
}
}
Because static inner classes are static members of external classes, static inner classes can only access static members of external classes. And the object that creates the static inner class does not depend on the outer class's object. The syntax for creating a static inner class object outside of an external class is as follows
< external class class name >.< inner class class name > Reference variable name = new < external class class name >.< inner class constructor >;
Example:
Class Outter {
Static Class Inner {
public void SayHello () {
System.out.println ("Successfully created static inner class object! ”);
}
}
public void Getinner () {
To create an object of a static inner class in an external class
Inner II = new Inner ();
Ii.sayhello ();
}
}
public class Maintest {
public static void Main (string[] args) {
To create an object of a static inner class outside the outer class
Outter.inner i = new Outter.inner ();
I.sayhello ();
objects that use static inner classes in an external class
New Outter (). Getinner ();
}
}
The static inner class is actually out of control of the outer class, and the creation of the object no longer requires the existence of an external class object, essentially a generic class that is placed in another class.

Anonymous inner class
An anonymous inner class based on inheritance, with the following syntax:
New < anonymous inner class to inherit the corresponding constructor of the parent class > {
Anonymous inner class class body
};
Based on an anonymous inner class that implements an interface, the syntax is as follows:
New < Interface name > {
Anonymous inner class class to implement all the methods in the interface
}
The outer variables used in the anonymous inner class are declared final.
Anonymous inner class object initialization code can be written in its non-static block
The. class file generated by the anonymous inner class is the < external class class name >$<n>.class N is the first anonymous inner class of the class.

Modifiers available for various internal classes
member Inner class
Final, abstract, public, private, protected, static
Static Inner class
Final, abstract, public, private, protected
Local inner class
Final, abstract
Anonymous inner class
You cannot use modifiers on anonymous inner classes

Internal interface
Internal interfaces defined in a class are static members whether or not the static adornments are.
The internal interface is declared as private, which means that it can only be implemented by an inner class in an external class.
Internal interface can not play a local role, or compile error. Because the interface is designed to be published externally, many classes are implemented, while local roles violate the design intent of the interface.
The internal interface in an interface is a member of an interface and has all the properties of an interface member and cannot be decorated with private.
The syntax for implementing an internal interface outside an external interface is as follows:
Class < classes name > implements < external interface name >.< Internal Interface name > {
Class Body
}

Some knowledge about the inner class of Java

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.