set Field Final and Static - Android java.lang.reflect

Android examples for java.lang.reflect:Field Static

Description

set Field Final and Static

Demo Code


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

public class Main {
    public static void setFinalStatic(Field field, Object newValue)
            throws Exception {
        field.setAccessible(true);//from   w w  w .  j a va  2  s .c om

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, newValue);
    }
}

Related Tutorials