Iterate over Static Fields and Values from a Java Class

Writing by on Monday, 5 of October , 2009 at 4:14 pm

Here’s a method that shows how to get all the static fields in a Java class and return them.

	public static List printFields(Class c) throws ClassNotFoundException, IllegalAccessException {
    		List fields = new ArrayList();
        java.lang.reflect.Field[] f = c.getDeclaredFields();
        for (java.lang.reflect.Field field : f) {
            int modifier = field.getModifiers();
            if (java.lang.reflect.Modifier.isStatic(modifier)){
          		String val = (String) field.get( null );
          		System.out.print(field.getName() +  ": " + val.toString() + ", ");
        return fields;

Leave a comment

Category: Java

No Comments

No comments yet.

Leave a comment

You must be logged in to post a comment.

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.