Is the scope of the variable, because the anonymous internal class appears inside a method. If it wants to access the variables defined in the parameters or methods of this method, these parameters and variables must be modified to final. Although the anonymous internal class is inside the method, the internal class is compiled into outer during actual compilation. inner, which indicates that the position of the internal class is at the same level as that of the method in the external class. The variables or parameters in the method in the external class are only partial variables of the method, the scope of these variables or parameters is only valid within this method. Because the internal class and method are at the same level during compilation, the variables or parameters in the method can be referenced only when they are final.
Java code:
Package com. cxz. j2se;
Public class myclass {
Public myclass (){
Final int finalvalue = 10;
Int not $ final = 20;
Myinterface = new myinterface (){
Public void functionwithoutpara (){
// Compile Error
// System. Out. println (nofinal );
System. Out. println (finalvalue );
}
Public void functionwithpara (INT num ){
System. Out. println ("the parameter" + num
+ "Has been passed by the method ");
}
};
Myinterface. functionwithoutpara ();
Myinterface. functionwithpara (not $ final );
System. Out. println (myinterface. getclass (). getname ());
}
Public static void main (string [] ARGs ){
New myclass ();
}
}
Ii. why can't the local internal class only access final variables?
In short, it is about scope. It is as if the variable defined in the method cannot be changed because you do not know that the local variable already exists in the method. In this internal class, the local variables in the method are invalid, that is, they are not in the scope, so they cannot be accessed.
But why can I use final for access?
Because Java adopts a copy local variable method, that is to say, it copies the local variable defined as final, and can also be referenced, but it cannot be re-assigned. This leads to the illusion that access local variable can be accessed. In this case, the assignment cannot be re-assigned, so unexpected events will not occur.
3. If a local internal class is defined and the local internal class uses an object defined externally, why does the compiler require its parameter reference to be final?
Note: Local internal classes include anonymous internal classes.
The reason is as follows:
Abstract class absclass {
Public abstract void print ();
}
Public class Test2 {
Public static void test (final string s) {// once the parameter is used inside the Anonymous class, it must be final
Absclass c = new absclass (){
Public void print (){
System. Out. println (s );
}
};
C. Print ();
}
Public static void main (string [] ARGs ){
Test ("Hello world! ");
}
}
Each process in JVM has multiple roots, each static variable, method parameter, and local variable. Of course, this is a guiding type. the basic type cannot be the root, but the root is actually a storage address. when the garbage collector is working, it first traverses the referenced objects from the root and marks them, so that the recursion is to the final end. After all the roots are traversed, the objects not marked are not referenced, objects that can be recycled (some objects have the finalized method, although not referenced, however, the JVM has a dedicated queue to reference them until the finalized method is executed, so it is not removed from the queue and can be recycled. This is irrelevant to the topic, including division of generation ). this looks good.
However, in the callback method of the internal class, S is neither a static variable nor a temporary variable in the method nor a method parameter. It cannot be used as the root, and no variable in the internal class references it, in the internal class external method, if the external variable s points to another object, the object s in the callback method loses the reference and may be recycled, because most internal class callback methods are executed in other threads, they may still be accessed after being recycled. what is the result?
The use of the final modifier will not only ensure that the object reference will not change, but also the compiler will continue to maintain the lifecycle of this object in the callback method. therefore, this is the fundamental significance of final variables and final parameters.