Class getField() method in Java with Examples
The getField() method of java.lang.Class class is used to get the specified field of this class, which is the field that is public and its members. The method returns the specified field of this class in the form of Field objects.
public Field getField(String fieldName) throws NoSuchFieldException, SecurityException
Parameter: This method accepts a parameter fieldName which is the Field to get.
Return Value: This method returns the specified field of this class in the form of Field objects.
Exception This method throws:
- NoSuchFieldException if a field with the specified name is not found.
- NullPointerException if name is null
- SecurityException if a security manager is present and the security conditions are not met.
Below programs demonstrate the getField() method.
Class represented by myClass: class Test obj Field of myClass: public java.lang.Object Test.obj
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.