Student Feng Weili asked me an interesting Java compilation question at noon today, and I could not explain it to him. I don't know if there are any passing experts who can explain the reason clearly. I hope you will not give me a question!
The second line of code in the main method of the following program is exactly the same as the two lines of code in the annotation. The two lines of code in the annotation cannot be compiled (this is easy to understand ), the second line (using method call chain) can be compiled smoothly (which is hard to understand ).
Public class test
{
Public void func ()
{
System. Out. println ("func ");
}
Public static void main (string ARGs []) throws exception
{
Object OBJ = new test ();
// The following line can be compiled successfully
(TEST) OBJ). getclass (). newinstance (). func ();
// The following two rows cannot be compiled
/* Class C = (TEST) OBJ). getclass ();
C. newinstance (). func ();*/
}
}
Thanks to the help of Mr. paulex, at the prompt of Mr. paulex, I basically understood the cause of the above problem. The following is the answer from Mr. paulex:
Bytes -----------------------------------------------------------------------------------------------------------------------
Paulex is published on 08:09:00 IP: 10.0.0. * because of generic, the compiler can obtain type information during compilation, so this type of code can be compiled. Change the following two lines
Class <? Extends test> C = (TEST) OBJ). getclass ();
C. newinstance (). func ();
It should be able to pass the compilation.
The 1.4.2 compiler is used upstairs, so it cannot be compiled.
The following is a further explanation of the Problem Based on my answers from Mr. paulex: after the model is introduced in JDK 1.5, the object. getclass () method is defined as follows:
Public final class <? Extends Object>
Getclass()
Returns the runtime class of an object. That ClassObject is the object that is locked Static synchronizedMethods of the represented class.
-
Returns:
-
The
java.lang.ClassObject that represents the runtime class of the object. The result is of Type Class<? extends X>Where X is the erasure of the static type of the expression on which getClassIs called.
This indicates that the object type returned by (TEST) OBJ). getclass () Statement is class <? Extends test>, while the newinstance () method of class <t> is defined as follows:
Public tNewinstance() Throws instantiationexception, illegalaccessexception
That is, for the compiler, the object type of the newinstance () method of class <Test> is test, and (TEST) OBJ). getclass () returns the object type as class <? Extends test>. Therefore, the compiler considers the object type returned by (TEST) OBJ). getclass (). newinstance () as test.
The following two lines of code cannot be compiled
Class C = (TEST) OBJ). getclass ();
C. newinstance (). func ();
It is because (TEST) OBJ). getclass () returns the object type as class <? Extends test>, But we forcibly convert the result into a class in the first line, and then call the newinstance method of the class, instead of calling the newinstance method of the class <Test>, of course, the compiler no longer considers the newinstance method of class to return the object test.
-------------------------------------------------------
Thanks again to Mr. paulex, whose blog address is http://blog.csdn.net/paulex