Local class-local class
1. Remove the "static" keyword in the declaration of a static member class and move its definition to the static method or static initialization of its external class.CodeSegment becomes a local static member class
2. A member class is a local member class by moving its definition to the instance method or instance initialization code of its external class.
Public class Outer {private int instancefield; Private Static int staticfield; // 1/4-define a local member class in the "instance initialization code segment" {int localvirable1 = 0; final int localvirable2 = 1; Class inner1 {public inner1 () {// you can directly access the instancefield of the external class = 1; outer. this. instancefield = 1; // system. out. print (localvirable1); // local variables cannot access system. out. print (localvirable2); // local constants can be accessed} // local classes cannot have permissions, because local class/* Public class inner2 {} */} is not used externally at all. // 2/4-define a local static member class static {class inner2 {public inner2 () {staticfield = 1; // The static domain that can access the external class // instancefield = 2; // The non-static domain cannot be accessed, because it has not been created} public void intancemethod () {// 3/4-define the local class inner3 {}// outer In the instance method. inner2 inner2; // The local class is valid only in its block} Private Static void staticmethod () {// 4/4-define the local class inner4 {public inner4 () {staticfield = 2 ;}} in the static method ;}} // The interface is not a local class/* interface I {}*/}}
-- Features
A local class can only access local variables declared as final in the code segment to which it belongs. This is because the code segment is returned after execution.
The local variable disappears, but the instance of the local class can still exist.
-- Constraint
1. Because the internal class is only visible in the code segment that defines it, there is no permission to set it.
2. Because of its small visibility, the interface is defined as meaningless.
3. duplicate names with external classes are not allowed
-- When to use
All are used in the form of anonymous classes