A strange problem in the JDK 1.5 compilation

Source: Internet
Author: User
Tags first row

Students today asked me a funny Java compiler problem, I can not explain to him, do not know if there is no passing the high man can explain why, look at the generous enlighten!

The second line of code in the Main method of the following program and the two lines of code in the comment are exactly the same. The two lines of code in the annotation are not compiled (which is easy to understand), while the second line (using the method call chain) can be successfully compiled (which is difficult 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 successfully compiled
(Test) obj. GetClass (). newinstance (). Func ();

The following two lines cannot be compiled
/*class C = ((Test) obj). GetClass ();
C.newinstance (). Func (); */
}
}

Thanks to Mr. Paulex's help, I have basically understood the cause of the above problems under Mr. Paulex's tips. Here is Mr. Paulex's answer:

Because generic, the compiler can get type information at compile time, so you can compile such code. You will change the following two lines into

class<? Extends test> C = ((Test) obj). GetClass ();
C.newinstance (). Func ();

Should be able to pass the compilation.

The following is a further explanation of the problem, based on Mr. Paulex's answer:

After the introduction of the paradigm 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. This class object is the object, is locked by static synchronized methods of the represented class.

Returns:
The Java.lang.Class object 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 getclass is called.

This means (Test) obj) The object type returned by the. GetClass () statement is class<? Extends Test>, while the class<t> newinstance () method is defined as follows:

Public T newinstance () throws Instantiationexception,illegalaccessexception

That is, the object type of the Newinstance () method to which the compiler appears to be,class<test> is Test, and ((Test) obj). GetClass () Returns the object type Class<? Extends Test>, so the compiler considers ((Test) obj). GetClass (). newinstance () The object type returned is Test.

The following two lines of code fail to compile

Class C = ((Test) obj). GetClass ();
C.newinstance (). Func ();

Because (Test) obj. GetClass () Returns an object type of class<? Extends Test>, but we cast the result in the first row into class, then call class's Newinstance method instead of calling Class<test> 's Newinstance method, The compiler certainly no longer thinks that the object returned by the Newinstance method of class is test.

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.