September 2019
Intermediate to advanced
816 pages
18h 47m
English
The solution to this problem relies on the Modifier.isPublic() and Modifier.isPrivate() methods.
Let's assume the following Melon class has two public fields and two private fields:
public class Melon { private String type; private int weight; public Peeler peeler; public Juicer juicer; ...}
First, we need to fetch the Field[] array corresponding to this class via the getDeclaredFields() method:
Class<Melon> clazz = Melon.class;Field[] fields = clazz.getDeclaredFields();
Field[] contains all the four fields from earlier. Further, let's iterate this array and let's apply Modifier.isPublic() and Modifier.isPrivate() flag methods to each Field:
List<Field> publicFields = new ArrayList<>();List<Field> ...