How to find out the declared type of an identifier in Java?

前端 未结 3 1718
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 01:38

I have a simple class Apple extends from another simple class Fruit.

At run-time, I could use

Fruit fruit = new Apple();

fruit.getClass();
<         


        
3条回答
  •  半阙折子戏
    2020-12-07 02:16

    No, there isn't: at least not using reflection. Reflection can give you information about an object at runtime, as well as fields, methods and classes, but not local variables. If fruit was a field, you could do something like the following:

    FruitBasket.class.getDeclaredField("fruit").getType();
    

提交回复
热议问题