arrays - Java Reflection API - Getting the value of a String[] field -
i wondering if possible grab string[] of unknown size class using java reflection api , store elsewhere?
thanks.
class foo { private string[] bar; public foo(string[] bar) { this.bar = bar; } public static void main(string[] args) throws exception { foo foo = new foo(new string[] {"a", "b", "c"}); field barfield = foo.class.getdeclaredfield("bar"); string[] bar = (string[]) barfield.get(foo); system.out.println(arrays.tostring(bar)); // [a, b, c] } } in addition getdeclaredfield(string), there's getfield(string) , getfields() return public fields getdeclaredfields() fields class declares.
Comments
Post a Comment