get Static Fields - Java Reflection

Java examples for Reflection:Field Get

Description

get Static Fields

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

public class Main {


    public static String getStaticFields(Class c) {

        Field[] fields = c.getDeclaredFields();
        StringBuffer sb = new StringBuffer();
        sb.append(c.getCanonicalName() + "[" + "\n");
        for (Field field : fields) {
            if (Modifier.isStatic(field.getModifiers())) {
                try {
                    sb.append("\t" + field.getName() + "="
                            + field.get(null).toString());
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }//from   www.  ja  v  a2  s.c  o  m
                sb.append("\n");
            }
        }
        sb.append("]" + "\n");
        return sb.toString();
    }
}

Related Tutorials