Java Class getDeclaredFields() Method



Description

The Java Class getDeclaredFields() method returns an array of Field objects including public, protected, default (package) access, and private fields, but excludes inherited fields. The method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.

Declaration

Following is the declaration for java.lang.Class.getDeclaredFields() method

public Field[] getDeclaredFields() throws SecurityException

Parameters

NA

Return Value

This method returns the array of Field objects representing all the declared fields of this class.

Exception

SecurityException − If a security manager, s, is present.

Getting Declared Fields of a Class Example

The following example shows the usage of java.lang.Class.getDeclaredFields() method. In this program, we've created an instance of ClassDemo and then using getClass() method, the class of the instance is retrieved. Using getDeclaredFields(), we've retrieved all declared Fields and printed them.

package com.tutorialspoint;

import java.lang.reflect.Field;

public class ClassDemo {

   public static void main(String[] args) {

      try {            
         ClassDemo c = new ClassDemo();
         Class cls = c.getClass();
       
         // returns the array of Field objects
         Field[] fields = cls.getDeclaredFields();
         for(int i = 0; i < fields.length; i++) {
            System.out.println("Field = " + fields[i].toString());
         }
      } catch(Exception e) {
         System.out.println(e.toString());
      }
   }

   public ClassDemo() {
      // no argument constructor
   }

   public ClassDemo(long l, int i) {
      this.l = l;
      this.i = i;
   }

   long l = 77688;
   int i = 3;
}

Output

Let us compile and run the above program, this will produce the following result −

Field = long com.tutorialspoint.ClassDemo.l
Field = int com.tutorialspoint.ClassDemo.i

Getting Declared Fields of an ArrayList Class Example

The following example shows the usage of java.lang.Class.getDeclaredFields() method. In this program, we've used class of ArrayList. Using getDeclaredFields(), we've retrieved all declared Fields and printed them.

package com.tutorialspoint;

import java.lang.reflect.Field;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         Class cls = ArrayList.class;
       
         // returns the array of Field objects
         Field[] fields = cls.getDeclaredFields();
         for(int i = 0; i < fields.length; i++) {
            System.out.println("Field = " + fields[i].toString());
         }
      } catch(Exception e) {
         System.out.println(e.toString());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Field = private static final long java.util.ArrayList.serialVersionUID
Field = private static final int java.util.ArrayList.DEFAULT_CAPACITY
Field = private static final java.lang.Object[] java.util.ArrayList.EMPTY_ELEMENTDATA
Field = private static final java.lang.Object[] java.util.ArrayList.DEFAULTCAPACITY_EMPTY_ELEMENTDATA
Field = transient java.lang.Object[] java.util.ArrayList.elementData
Field = private int java.util.ArrayList.size

Getting Declared Fields of a Thread Class Example

The following example shows the usage of java.lang.Class.getDeclaredFields() method. In this program, we've used class of Thread. Using getDeclaredFields(), we've retrieved all declared Fields and printed them.

package com.tutorialspoint;

import java.lang.reflect.Field;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         Class cls = Thread.class;
       
         // returns the array of Field objects
         Field[] fields = cls.getDeclaredFields();
         for(int i = 0; i < fields.length; i++) {
            System.out.println("Field = " + fields[i].toString());
         }
      } catch(Exception e) {
         System.out.println(e.toString());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Field = private volatile long java.lang.Thread.eetop
Field = private final long java.lang.Thread.tid
Field = private volatile java.lang.String java.lang.Thread.name
Field = volatile boolean java.lang.Thread.interrupted
Field = private volatile java.lang.ClassLoader java.lang.Thread.contextClassLoader
Field = private java.security.AccessControlContext java.lang.Thread.inheritedAccessControlContext
Field = private final java.lang.Thread$FieldHolder java.lang.Thread.holder
Field = java.lang.ThreadLocal$ThreadLocalMap java.lang.Thread.threadLocals
Field = java.lang.ThreadLocal$ThreadLocalMap java.lang.Thread.inheritableThreadLocals
Field = private java.lang.Object java.lang.Thread.scopedValueBindings
Field = private static final java.lang.Object java.lang.Thread.NEW_THREAD_BINDINGS
Field = final java.lang.Object java.lang.Thread.interruptLock
Field = private volatile java.lang.Object java.lang.Thread.parkBlocker
Field = volatile sun.nio.ch.Interruptible java.lang.Thread.nioBlocker
Field = public static final int java.lang.Thread.MIN_PRIORITY
Field = public static final int java.lang.Thread.NORM_PRIORITY
Field = public static final int java.lang.Thread.MAX_PRIORITY
Field = private jdk.internal.vm.Continuation java.lang.Thread.cont
Field = static final int java.lang.Thread.NO_INHERIT_THREAD_LOCALS
Field = private static final java.lang.StackTraceElement[] java.lang.Thread.EMPTY_STACK_TRACE
Field = private volatile java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.uncaughtExceptionHandler
Field = private static volatile java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.defaultUncaughtExceptionHandler
Field = long java.lang.Thread.threadLocalRandomSeed
Field = int java.lang.Thread.threadLocalRandomProbe
Field = int java.lang.Thread.threadLocalRandomSecondarySeed
Field = private jdk.internal.vm.ThreadContainer java.lang.Thread.container
Field = private volatile jdk.internal.vm.StackableScope java.lang.Thread.headStackableScopes
java_lang_class.htm
Advertisements