See if field exists in class

后端 未结 6 1875
眼角桃花
眼角桃花 2021-02-03 21:47

I have a class with various variables

public class myClass{

    public int id;
    public String category;
    public String description;
    public String star         


        
6条回答
  •  滥情空心
    2021-02-03 22:23

    Your compiler usually knows that pretty well, and the runtime lets you examine loaded classes with reflection.

    Object someObject = ...
    Class someClass = someObject.getClass();
    Field someField = someClass.getField("foo");
    

    The getField() method will throw an exception if the field can not be found.

提交回复
热议问题