September 2019
Intermediate to advanced
816 pages
18h 47m
English
All the fields of a class are accessible via the Class.getDeclaredFields() method. This method returns an array of Field:
Field[] fields = clazz.getDeclaredFields();// final java.lang.Object modern.challenge.Pair.left// final java.lang.Object modern.challenge.Pair.rightSystem.out.println("Fields: " + Arrays.toString(fields));
For fetching the actual name of the fields, we can easily provide a helper method:
public static List<String> getFieldNames(Field[] fields) { return Arrays.stream(fields) .map(Field::getName) .collect(Collectors.toList());}
Now, we only receive the names of the fields:
List<String> fieldsName = getFieldNames(fields);// left, rightSystem.out.println("Fields names: " + fieldsName);
Getting ...