What is meant by “the erasure of the static type of the expression on which it is called” in the getClass() docs?

前端 未结 2 703
时光说笑
时光说笑 2020-12-16 18:50

The documentation for the \"public final Class getClass()\" method of Object says:

The actual result type is Class where

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 19:26

    Let's consider the following example:

    List test = new ArrayList();
    Class clazz = test.getClass();
    

    the static type of test (the expression on which getClass() is called) is List of which the erasure is List (see type erasure). Note that the dynamic (or runtime) type of test is ArrayList, and the runtime type of clazz will be Class.

    So, Class in this case is Class.

提交回复
热议问题