Recent Tutorials and Articles
    Listing class fields using Java Reflections API
    Published on: 10th August 2013

    This tutorial provides the code to list down all (private, protected, default and public) fields (both instance and static) of a class using Reflections API in Java.

    Reflections API is a powerful tool provided by Java to developers as it lets you access all (with any access modifier - public, private, protected and default) the members (instance and static fields & methods), provided you are not running in restricted context e.g. Applets where Security manager will not let you do most of reflections operations.

    Below is the program to print all the fields of a class using Reflections API in Java.

    FieldsPrinter.java
    package com.sts.allprogrammingtutorials.generics;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class FieldsPrinter {
    	
    	public void print(Class<?> clazz) {
    		
    		for (Field field : clazz.getDeclaredFields()) { // get all the methods and iterate over those
    			//nested class to build the method components
    			final FieldComponentsBuilder fieldComponentsBuilder = new FieldComponentsBuilder(field);
    			
    			try {
    				//Check if the field is accessible with compiler check in place, 
    				//if not set the accessibility to true by passing the compiler checks.
    				if(!field.isAccessible()) {
    					field.setAccessible(true);
    				}
    				System.out.println(fieldComponentsBuilder.getModifier() + " "
    						+ fieldComponentsBuilder.getType() + " "
    						+ field.getName() + "=" + field.get(fieldComponentsBuilder.getType()));
    			} catch (IllegalArgumentException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IllegalAccessException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		
    	}
    	
    	private static class FieldComponentsBuilder {
    		private final Field field;
    		
    		public FieldComponentsBuilder(final Field field) {
    			this.field = field;
    		}
    		
    		
    		/**
    		 * @return modifiers such as public, private, protected, static etc.
    		 */
    		public String getModifier() {
    			return Modifier.toString(field.getModifiers());
    		}
    		
    		/**
    		 * @return the type of a field
    		 */
    		public String getType() {
    			return field.getType().getCanonicalName();
    		}
    		
    	}
    }
    
    

    Following is the demo class that calls the above program by passing in the class which we want to print all the fields of -

    FieldsPrinterDemo.java
    package com.sts.allprogrammingtutorials.generics;
    
    
    /**
     * @author Sain Technology Solutions
     * 
     */
    public class FieldsPrinterDemo {
    
    	/**
    	 * @param args
    	 * @throws ClassNotFoundException 
    	 */
    	public static void main(String[] args) throws ClassNotFoundException {
    		
    		final Class<?> clazz = Class.forName("java.lang.String");
    
    		FieldsPrinter fieldsPrinter = new FieldsPrinter();
    		fieldsPrinter.print(clazz);
    
    	}
    
    }
    
    

    And here is what you get as output after running the above code -

    Output:
    private final char[] value=[[email protected]
    private final int offset=0
    private final int count=3
    private int hash=0
    private static final long serialVersionUID=-6849794470754667710
    private static final java.io.ObjectStreamField[] serialPersistentFields=[Ljava.io.ObjectStreamField;@7c6768
    public static final java.util.Comparator CA[email protected]1690726
    
    

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 10th August 2013

    Comment Form is loading comments...