getDeclaredField(String): This method is available in the java.lang.Class of Java.
Syntax:
Field java.lang.Class.getDeclaredField(String name) throws NoSuchFieldException, SecurityException
This method takes one argument. This method returns a Field object that reflects the specified field of the class or interface represented by this Class object. The name parameter is a String that specifies the simple name of the desired field.
Parameters: One parameter is required for this method.
name: the name of the field.
Returns: the Field object for the specified field in this class.
Throws:
1. NoSuchFieldException - if a field with the specified name is not found.
2. NullPointerException - if the name is null.
3. SecurityException - If a security manager, s, is present and any of the following conditions are met:
a). the caller's class loader is not the same as the class loader of this class and invocation of the s.checkPermission method with RuntimePermission("accessDeclaredMembers")denies access to the declared field.
b). the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class.
Approach 1: When no exception
Java
package com.Class;public class ClassgetDeclaredField {public static void main(String[] args) throwsClassNotFoundException,NoSuchFieldException, SecurityException {@SuppressWarnings("rawtypes")Class class1 = Class.forName("java.lang.String");String name = "coder";System.out.println(class1.getDeclaredField(name));}}
Output:
private final byte java.lang.String.coder
Approach 2: NoSuchFieldException
Java
package com.Class;public class ClassgetDeclaredField {public static void main(String[] args) throwsClassNotFoundException,NoSuchFieldException, SecurityException {@SuppressWarnings("rawtypes")Class class1 = Class.forName("java.lang.String");String name = "hello";System.out.println(class1.getDeclaredField(name));}}
Output:
Exception in thread "main" java.lang.NoSuchFieldException: hello at java.base/java.lang.Class.getDeclaredField(Class.java:2569) at com.Class.ClassgetDeclaredField.main(ClassgetDeclaredField.java:12)
Approach 3: NullPointerException
Java
package com.Class;public class ClassgetDeclaredField {public static void main(String[] args) throwsClassNotFoundException,NoSuchFieldException, SecurityException {@SuppressWarnings("rawtypes")Class class1 = Class.forName("java.lang.String");String name = null;System.out.println(class1.getDeclaredField(name));}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.lang.Class.getDeclaredField(Class.java:2562) at com.Class.ClassgetDeclaredField.main(ClassgetDeclaredField.java:12)
Some other methods of Class
getDeclaredConstructors(): This method returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.
getDeclaredField(String): This method returns a Field object that reflects the specified field of the class or interface represented by this Class object.
getDeclaredFields(): This method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
getDeclaredMethod(String, Class...): This method returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
getDeclaredMethods(): This method returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.
getDeclaringClass(): If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.
getEnclosingClass(): This method returns the immediately enclosing class of the underlying class.
getEnclosingConstructor(): If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class.
getEnclosingMethod(): If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.
getEnumConstants(): This method returns the elements of this enum class or null if this class object does not represent an enum type.
getField(String): his method returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.
getFields(): This method returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
getGenericInterfaces(): This method returns the Types representing the interfaces directly implemented by the class or interface represented by this Class object.
getGenericSuperclass(): This method returns the Type representing the direct superclass of the entity (class, interface, primitive type, or void) represented by this Class object.
getInterfaces(): This method returns the interfaces directly implemented by the class or interface represented by this Class object.
getMethod(String, Class...): This method returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.
getModifiers(): This method returns the Java language modifiers for this class or interface, encoded in an integer.
getModule(): This method returns the module that this class or interface is a member of.
getName(): This method returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object.
getNestHost(): This method returns the nest host of the nest to which the class or interface represented by this Class object belongs.
getNestMembers(): This method returns an array containing Class objects representing all the classes and interfaces that are members of the nest to which the class or interface represented by this Class object belongs.
getPackage(): This method gets the package of this class.
getPackageName(): This method returns the fully qualified package name.
getProtectionDomain(): This method returns the ProtectionDomain of this class.
getRecordComponents(): This method returns an array of RecordComponent objects representing all the record components of this record class, or null if this class is not a record class.
No comments:
Post a Comment