I have a POJO with 6 properties as below. i need get only 5 field names using reflection.
class Employee {
private Long id;
private String address;
private String phone;
private String firstName;
private String lastName;
private String designation;
//getters and setters
}
but i would like to get only 5 field names except ‘phone’ using java reflection getFields(). how can i get the field names? is it possible to get only specific field names instead of all field names from the pojo?
Thanks!
You can pass the name of your field to
getField()method: –but that will only fetch you
public fields.You can get
private fields, by usinggetDeclaredField("fieldName"):UPDATE: –
If you don’t have field names, then you have to iterate through all the fields and filter the array using the field name manually by using
Field.getName()method. There is no other way out: –