Java has introduced internal classes since jdk1.1. For more information, see the code. The advantage is that you can be lazy when designing classes. It can be used to reference other internal elements of a class. It is similar to taking this internal class as an element of the original class. There are also some design details that can be hidden, but there are still many advantages.
Define two interfaces
Package interfacepackage;
Public interface destination {
String readlabel ();
}
Package interfacepackage;
Public interface contents {
Int value ();
}
A class with test code
Package debug;
Import interfacepackage. contents;
Import interfacepackage. destination;
Public class tester {
Private int valuerate = 2;
Private class Pcontent implements contents {
Private int I = 11 * valuerate;
Public int value (){
Return I;
}
}
Protected class pdestination implements destination {
Private string label;
Private pdestination (string whereto ){
Label = whereto;
}
Public String readlabel (){
Return label;
}
}
Public destination DEST (string s ){
Return new pdestination (s );
}
Public contents cont (){
Return new Pcontent ();
}
Public static void main (string ARGs [])
{
Tester P = new Tester ();
Contents c = P. cont ();
System. Out. println (C. Value ());
Destination d = P. DEST ("fire outside the sky ");
System. Out. println (D. readlabel ());
System. Out. println ("done ");
}
}
The above code is an internal dynamic class. Can the internal static class be used? The answer is yes, but the static internal class cannot reference other non-static elements of the class. For example, if the Pcontent internal class in the above example is changed to static class, the valuerate attribute cannot be referenced, in this case, a compilation error will be reported, but it can be run if valuerate is changed to static.