How to test whether the value of a Java field gotten by reflection is null?

前端 未结 4 1276
北恋
北恋 2021-01-15 16:42

I have

Field f = this.getClass().getFields()[0];

I need to know if f\'s value in this is null or not

4条回答
  •  迷失自我
    2021-01-15 16:50

    You need to get the field from the object then check if it is null

    Field f = this.getClass().getFields()[0];
    if (f.get(this) == null)
      ...
    

提交回复
热议问题