Field value

Report a typo

Implement getFieldValue method to retrieve the value of public field fieldName from the given object. If the field doesn't exist or it is not available (i.e. has private access modifier), return null.

Remember that get() method may throw an exception if you try to access a private field.

Sample Input 1:

FieldGetter class

Sample Output 1:

Well done!
Write a program in Java 17
import java.lang.reflect.Field;
/**
* Get value for a given public field or null if the field does not exist or is not accessible.
*/
class FieldGetter {

public Object getFieldValue(Object object, String fieldName) throws IllegalAccessException {

}

}
___

Create a free account to access the full topic